Skip to content

Commit 638124e

Browse files
committed
fix style to be stroustrup/k&r
1 parent a1307bc commit 638124e

File tree

1 file changed

+49
-42
lines changed
  • chapters/fundamental_algorithms/euclidean_algorithm

1 file changed

+49
-42
lines changed

chapters/fundamental_algorithms/euclidean_algorithm/euclidean.md

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,38 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout
6969
#include <cmath>
7070

7171
// Euclidean algorithm using modulus
72-
int euclid_mod(int a, int b) {
72+
int euclid_mod(int a, int b)
73+
{
7374
a = std::abs(a);
7475
b = std::abs(b);
7576
while (b != 0) {
76-
auto temp = b;
77-
b = a % b;
77+
int temp = b;
78+
b = a%b;
7879
a = temp;
7980
}
8081

8182
return a;
8283
}
8384

8485
// Euclidean algorithm with subtraction
85-
int euclid_sub(int a, int b) {
86+
int euclid_sub(int a, int b)
87+
{
8688
a = std::abs(a);
8789
b = std::abs(b);
8890
while (a != b) {
8991
if (a > b) {
9092
a -= b;
91-
} else {
93+
}
94+
else {
9295
b -= a;
9396
}
9497
}
9598

9699
return a;
97100
}
98101

99-
int main() {
102+
int main()
103+
{
100104
auto check1 = euclid_mod(64*67, 64*81);
101105
auto check2 = euclid_sub(128*12, 128*77);
102106

@@ -111,42 +115,43 @@ int main() {
111115
#include <stdio.h>
112116
#include <math.h>
113117
114-
int euclid_mod(int a, int b){
118+
int euclid_mod(int a, int b)
119+
{
115120
a = abs(a);
116121
b = abs(b);
117122
118-
int temp;
119123
while (b != 0){
120-
temp = b;
124+
int temp = b;
121125
b = a%b;
122126
a = temp;
123127
}
124128
125129
return a;
126130
}
127131
128-
int euclid_sub(int a, int b){
132+
int euclid_sub(int a, int b)
133+
{
129134
a = abs(a);
130135
b = abs(b);
131136
132-
while (a != b){
133-
if (a > b){
134-
a = a - b;
137+
while (a != b) {
138+
if (a > b) {
139+
a -= b;
135140
}
136-
else{
137-
b = b - a;
141+
else {
142+
b -= a;
138143
}
139144
}
140145
141146
return a;
142147
}
143148
144-
int main(){
145-
146-
int check = euclid_mod(64*67, 64*81);
149+
int main()
150+
{
151+
int check1 = euclid_mod(64*67, 64*81);
147152
int check2 = euclid_sub(128*12, 128*77);
148153
149-
printf("%d\n", check);
154+
printf("%d\n", check1);
150155
printf("%d\n", check2);
151156
}
152157
@@ -235,7 +240,7 @@ print euclid_sub(128 * 12, 128 * 77)
235240
### C#
236241

237242
```cs
238-
// submitted by Julian Schacher
243+
// submitted by Julian Schacher
239244
using System;
240245

241246
namespace Euclidean_Algorithm
@@ -321,34 +326,36 @@ main = do
321326

322327
```rust
323328
fn euclid_sub(mut a: i64, mut b: i64) -> i64 {
324-
a = a.abs();
325-
b = b.abs();
326-
while a != b {
327-
if a < b {
328-
b -= a;
329-
} else {
330-
a -= b;
329+
a = a.abs();
330+
b = b.abs();
331+
while a != b {
332+
if a < b {
333+
b -= a;
334+
} else {
335+
a -= b;
336+
}
331337
}
332-
}
333-
a
338+
339+
a
334340
}
335341

336342
fn euclid_rem(mut a: i64, mut b: i64) -> i64 {
337-
a = a.abs();
338-
b = b.abs();
339-
while b != 0 {
340-
let tmp = b;
341-
b = a % b;
342-
a = tmp;
343-
}
344-
a
343+
a = a.abs();
344+
b = b.abs();
345+
while b != 0 {
346+
let tmp = b;
347+
b = a % b;
348+
a = tmp;
349+
}
350+
351+
a
345352
}
346353

347354
fn main() {
348-
let chk1 = euclid_rem(64 * 67, 64 * 81);
349-
let chk2 = euclid_sub(128 * 12, 128 * 77);
350-
println!("{}", chk1);
351-
println!("{}", chk2);
355+
let chk1 = euclid_rem(64 * 67, 64 * 81);
356+
let chk2 = euclid_sub(128 * 12, 128 * 77);
357+
println!("{}", chk1);
358+
println!("{}", chk2);
352359
}
353360
```
354361

@@ -414,5 +421,5 @@ public static int euclidMod(int a, int b) {
414421
}
415422

416423
return a;
417-
}
424+
}
418425
```

0 commit comments

Comments
 (0)