Skip to content

Commit ff4ab88

Browse files
committed
Merge branch 'master' of github.com:leios/algorithm-archive
2 parents adb79fd + ea1b922 commit ff4ab88

File tree

1 file changed

+163
-0
lines changed
  • chapters/fundamental_algorithms/euclidean_algorithm

1 file changed

+163
-0
lines changed

chapters/fundamental_algorithms/euclidean_algorithm/euclidean.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,166 @@ int main(){
114114
}
115115

116116
```
117+
118+
### C
119+
```c
120+
#include <stdio.h>
121+
#include <math.h>
122+
123+
int euclid_mod(int a, int b){
124+
125+
int temp;
126+
while (b != 0){
127+
temp = b;
128+
b = a%b;
129+
a = temp;
130+
}
131+
132+
return a;
133+
}
134+
135+
int euclid_sub(int a, int b){
136+
137+
while (a != b){
138+
if (a > b){
139+
a = a - b;
140+
}
141+
else{
142+
b = b - a;
143+
}
144+
}
145+
146+
return a;
147+
}
148+
149+
int main(){
150+
151+
int check = euclid_mod(64*67, 64*81);
152+
int check2 = euclid_sub(128*12, 128*77);
153+
154+
printf("%d\n", check);
155+
printf("%d\n", check2);
156+
}
157+
158+
```
159+
160+
### JavaScript
161+
162+
```html
163+
<!DOCTYPE html>
164+
<html>
165+
<body>
166+
<script>
167+
function euclid_mod(a, b){
168+
169+
var temp;
170+
while (b != 0){
171+
temp = b;
172+
b = a%b;
173+
a = temp;
174+
}
175+
176+
return a;
177+
}
178+
179+
function euclid_sub(a, b){
180+
181+
while (a != b){
182+
if (a > b){
183+
a = a - b;
184+
}
185+
else{
186+
b = b - a;
187+
}
188+
}
189+
190+
return a;
191+
}
192+
193+
document.write(euclid_mod(64*67, 64*81) + "<br>");
194+
document.write(euclid_sub(128*12, 128*77) + "<br>");
195+
</script>
196+
</body>
197+
</html>
198+
```
199+
200+
### Python
201+
202+
```python
203+
204+
# euclidean.py
205+
206+
def euclid_mod(a, b):
207+
208+
temp = 0
209+
210+
while b > 0:
211+
temp = b
212+
b = a % b
213+
a = temp
214+
215+
return a
216+
217+
def euclid_sub(a, b):
218+
219+
while a != b:
220+
if a > b:
221+
a = a - b
222+
else:
223+
b = b - a
224+
225+
return a
226+
227+
print euclid_mod(64 * 67, 64 * 81)
228+
print euclid_sub(128 * 12, 128 * 77)
229+
```
230+
231+
### C#
232+
233+
```cs
234+
// submitted by Julian Schacher‏
235+
using System;
236+
237+
namespace Euclidean_Algorithm
238+
{
239+
class Program
240+
{
241+
static void Main(string[] args)
242+
{
243+
int check = Algorithms.EuclidMod(64 * 67, 64 * 81);
244+
int check2 = Algorithms.EuclidSub(128 * 12, 128 * 77);
245+
246+
Console.WriteLine(check);
247+
Console.WriteLine(check2);
248+
}
249+
}
250+
251+
public static class Algorithms
252+
{
253+
public static int EuclidSub(int a, int b)
254+
{
255+
while (a != b)
256+
{
257+
if (a > b)
258+
a = a - b;
259+
else
260+
b = b - a;
261+
}
262+
263+
return a;
264+
}
265+
266+
public static int EuclidMod(int a, int b)
267+
{
268+
while (b != 0)
269+
{
270+
var temp = b;
271+
b = a % b;
272+
a = temp;
273+
}
274+
275+
return a;
276+
}
277+
}
278+
}
279+
```

0 commit comments

Comments
 (0)