QUICK

QUICK

WRITE A PROGRAM IN YOUR FAVORITE LANGUAGE THAT ASKS THE USER TO INPUT A BUNCH OF INTEGERS AND CALCULATES THE GREATEST COMMON DIVISOR

OR THIS BIRD IS GONNA STAB YOU

def gcd(a,b):
if b < a:
return gcd(b,a)
if a == 0:
return b
return gcd(b % a,a)

def gcf(numbers):
if len(numbers) < 1:
return -1
if len(numbers) == 1:
return numbers[0]
div = gcd(numbers.pop(),numbers.pop())
while len(numbers) > 0:
div = gcd(div,numbers.pop())
return div

def ask():
print("Input positive integers or be stabbed:")
numbers = []
try:
while True:
number = input()
number = int(number)
numbers.append(number)
except:
pass
print(gcf(numbers))

ask()

>mods banning me for complaining about captcha
I'M SO SORRY MODS CAPTCHA IS THE BEST MODS ARE THE BEST I LOVE THE NEW CAPTCHA

Shit thread

...

Fuck off already

BIRDMIN NO

gcf 0 _ = 1
gcf n ns = if and (map (test y) ns) then n else gcf (y - 1) ns
where test a b = b `mod` a == 0
y = abs n

main = do
raw > getLine
let ints = map read (words raw) :: [Int]
print $ gcf (minimum ints) ints

[gabbiel@maga ~]$ hs gcd.hs
type space separated list of integers: 45 54
9
[gabbiel@maga ~]$ hs gcd.hs
type space separated list of integers: 36 -6
6
[gabbiel@maga ~]$ hs gcd.hs
type space separated list of integers: 9 0 7
1
[gabbiel@maga ~]$ hs gcd.hs
type space separated list of integers: 28374 123746 13487
1

int gcd(unsigned int u, unsigned int v)
{

if (u == v)
return u;

if (u == 0)
return v;

if (v == 0)
return u;

if (~u & 1)
{
if (v & 1)
return gcd(u >> 1, v);
else
return gcd(u >> 1, v >> 1) > 1);

if (u > v)
return gcd((u - v) >> 1, v);

return gcd((v - u) >> 1, u);
}


int main(int argc, char** argv) {





int n =0;
int *arr;
int v;
int gcdt;

printf("enter the number of integers you want");
scanf("%d" , &n);
arr = malloc(sizeof(int) * n);
printf("\n enter the numbers user");

for (int i = 0 ; i < n ; i++){

if(scanf("%d " ,&v) ==1){

arr[i] = v;

}

}

for (int i = 0 ; i < n - 1 ; i++){

if ( i == 0)
gcdt = gcd(arr[i] , arr[i+1]);
if( i > 0){

gcdt = gcd(gcdt,arr[i+1]);

}


}

printf("\ngcd is %d" , gcdt);


return (EXIT_SUCCESS);
}

I cheated sorry

clojure

(defn my-gcd
[nums & {:keys [guess]}]
(loop [guess (or guess
(apply min nums))]
(cond (every? #(zero? (mod % guess))
nums) guess
:else (recur (dec guess)))))

(my-gcd '(288 192)) ;; => 96
(my-gcd '(999 666)) ;; => 333

These threads are chemo.

Pls no stab me furious bird.

emacs lisp

(defun try-gcd (lst guess)
(cond ((zerop (apply #'+ (mapcar (lambda (x) (mod x guess))
lst)))
guess)
(t (try-gcd lst (1- guess)))))

(defun my-gcd (&rest lst)
(try-gcd lst
(apply #'min lst)))

(my-gcd 288 192) ;; => 96
(my-gcd 999 666) ;; => 333

racket

(define (try-gcd lst guess)
(cond ((foldl (lambda (x y)
(and x y))
#t
(map (lambda (x)
(zero? (modulo x guess)))
lst))
guess)
(else (try-gcd lst (sub1 guess)))))

(define (my-gcd lst)
(try-gcd lst
(apply min lst)))

function gcdone(nums)
modx = minimum(nums);
hix = maximum(nums);
tempx = 2;
while(tempx > 1)
tempx = hix % modx;
hix = modx;
modx = tempx;
end;
return hix;
end

function gcdchain(nums)
lastx = gcdone(nums[1:2]);
for(iter = 3:length(nums))
lastx = gcdone([lastx, nums[iter]])
end;
return lastx;
end

fuck, there is a more elegant way, here...

clojure

(defn gcd
[x y]
(if (zero? y)
x
(recur y
(mod x y))))

(defn gcd*
[& lst]
(reduce gcd
lst))

(gcd* 27 81 18) ;; => 9
(gcd* 1332 999 666) ;; => 333

(defun gcd (a b)
(cond ((< b a) (gcd b a))
((= a 0) b)
(T (gcd (- b a) a))))

(defun main ()
(princ
(reduce #'gcd
(remove-if-not #'numberp
(mapcar #'read-from-string (cdr sb-ext:*posix-argv*))))))

>no function call

enjoy your library

...

Where is the user input you stupid frog? Do you want to get stabbed this badly?

npm install common-divisor --save


I love JavaScript

I have a library?

>Pls no stab me furious bird.

Then get to work.

>quick, do my homework

fuck off to /dpt/

that is messy as fuck code

>JavaScript
This isn't skid coding class. Fuck off.

I like these threads. The programs are simple enough that I can get practice with NASM without taking too much time.

EXTERN printf
EXTERN scanf
GLOBAL _start
%define size r9d
%define EOF -1

SECTION .data
begin: DB "Enter some numbers", `\n`, `\0`
result: DB "GCD is %d", `\n`, `\0`
input: DB "%d", `\0`
SECTION .bss
numbers: RESD 100 ; Too hard
SECTION .text

%MACRO divide 2
mov eax, %1
cdq
idiv %2
%ENDMACRO

%MACRO GCD 2
test %2, %2
jz %%end
%%continue:
mov ecx, %2
divide %1, %2
mov %2, edx
mov %1, ecx
test %2, %2
jnz %%continue
%%end:
%ENDMACRO

_start:
mov rdi, begin
call printf

xor rbx, rbx
get_input:
mov rdi, input
lea rsi, [numbers+rbx*4]
call scanf
add ebx, 1
cmp eax, EOF
jnz get_input

mov size, ebx
mov rbx, 1
mov edi, [numbers]
get_gcd:
mov esi, [numbers+rbx*4]
GCD edi, esi
add ebx, 1
cmp ebx, size
jl get_gcd

mov esi, edi
mov rdi, result
call printf

mov eax, 60
mov ebx, 0
syscall

>QUICK Sup Forums DO MY HOMEWORK OR LE BIRD WILL LE STABB U :DD

>you all fall for it

jesus christ

gcd(A, 0) ->
A;
gcd(A, B) ->
gcd(B, A rem B).

I have literally no comprehension of what the fuck that code does.

*goto behind you*

best solution

...

These are never hard to code, shut the hell up

>implying 101 classes have homework assignments that are hard

Better Scheme implementation:
(define (gcd-algorithm a b)
(if (= b 0)
a
(gcd-algorithm b (modulo a b))))

(define (gcd a b)
(if (< a b)
(gcd-algorithm b a)
(gcd-algorithm a b)))

(fold gcd 0 (read))

# Greatest common divisor (Euklid)

p "Enter two numbers, plox:"
a,b = gets.chomp.split(' ').map {|i| i.to_i}
until (a==0 || b==0) do a>b ? a%=b : b%=a end
p 'Greatest common divisor: ' + (a>b ? a : b).to_s

Shen:
(define fold
{(B --> A --> B) --> B --> (list A) --> B}
F Z [] -> Z
F Z [X | Y] -> (fold F (F Z X) Y))

(define gcd*
{number --> number --> number}
A B -> (gcd* B A) where (> B A)
A 0 -> A
A B -> (gcd* B (mod A B)))

(fold (function gcd*) 0 [10 20 30])

Mathematica. Brings up a dialog box. Input numbers should be separated by spaces.
GCD @@ FromDigits /@ StringSplit[InputString[]]

(DEFUN GCD (ARG &REST ARGS)
(PROG (A B)
(IF (NULL ARGS)
(RETURN ARG))
START
(IF (< ARG (CAR ARGS))
(SETF A ARG B (CAR ARGS))
(SETF B ARG A (CAR ARGS)))
(SETF ARGS (CDR ARGS))
LOOP
(IF (ZEROP A)
(SETF ARG B)
(LET ((TEMP B))
(SETF B (REM B A))
(SETF A TEMP)
(GO LOOP)))
(IF ARGS
(GO START))
(RETURN ARG)))

It even works, best solution in the whole thread.

yeah sorry about that, i'm still like 2 weeks new to C, and haven't program anything for years.

All of you are complaining but OP posts their own solution most of the time. The exercises are nice too.

nice.

I have hacked together this inefficient piece of shit.
[spoiler]Please don't stab me[/spoiler]"

import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
ArrayList list = new ArrayList();
Scanner scan = new Scanner(System.in);
System.out.println("Please input your integers by typing them in and pressing enter, one integer at a time.");
System.out.println("When finished, input anything that is not an integer");
String input = scan.nextLine();
boolean going = true;
while (going == true){
try {
int in = Integer.parseInt(input);
ArrayList inArray = getFactors(in);
list.add(inArray);
input = scan.nextLine();
}
catch (Exception e)
{
int gcf =(findGCF(list));
if (gcf != -1){
System.out.print("The Greatest Common Factor is: " + gcf);
}
else
{
System.out.print("There is no common factor between the given list of integers");
}
going = false;
}
}
}
public static int findGCF(ArrayListfactorLists)
{
int smallestArrayIndex = 0;
for (int i=0; i0; i--)
{
Integer currentFactor = factorLists.get(smallestArrayIndex).get(i-1);
int gcfFoundCounter = factorLists.size();
for(int j=0; j

...

# heuristics
print 1

C++
void euclidean(int a, int b) {
int x = a / (double)b;
int y = a % b;

if (y != 0) {
euclidean(b, y);
}
else {
cout

>INPUT A BUNCH OF INTEGERS
I can't read, disregard this post

>boolean going = true;
> while (going == true){

Dude, one of us is getting stabbed and it's not me.

#include

unsigned int gcd(unsigned int a, unsigned int b)
{
return b ? gcd(b, a % b) : a;
}

int main(void)
{
int denom = 0;
printf("Enter a positive integer: ");
for(int n;scanf("%d",&n) == 1;printf("Enter a positive integer: "))
{
if (n < 1)
break;
denom = denom == 0 ? n : gcd(n,denom);
}
if (denom < 1)
printf("No GCD\n");
else
printf("GCD is %d\n", denom);
return 0;
}

>int TheGCF = -1;
>return TheGCF;

Why not Mathematica? Fill in an array called list and then the code is:

GCD[list]

...

I'm impressed at how unreadable you managed to make this. Still readable because it's Ruby, though. Also, FYI:

>map(&:to_i)

>"Greatest common divisor: #{a>b ? a : b}"

test():
how do I type code

```this?```
"""this?"""
this?

You're hired.

[c0de]

[/c0de]

write #code in the options field

So when is that while True loop going to break so that you can actually print the gcf?

This

Its ok, just compile it and execute it. stope asking questions.

ABAP
REPORT z_gcd.

DATA gcd TYPE p LENGTH 16.

PARAMETERS:
a LIKE gcd LENGTH 16 OBLIGATORY DEFAULT 744480,
b LIKE gcd LENGTH 16 OBLIGATORY DEFAULT 1231776,
c LIKE gcd LENGTH 16 OBLIGATORY DEFAULT 4372128.

START-OF-SELECTION.

PERFORM euclid USING a b CHANGING gcd.
PERFORM euclid USING gcd c CHANGING gcd.

WRITE: 'The gcd is', gcd.

*&---------------------------------------------------------------------*
FORM euclid
USING VALUE(a) LIKE gcd
VALUE(b) LIKE gcd
CHANGING result LIKE gcd.

DATA h LIKE a.

WHILE b NE 0.
h = a MOD b.
a = b.
b = h.
ENDWHILE.

result = a.
ENDFORM. "euclid

Prolog
% gcd of 2 numbers
gcd(A, 0, A):-!.
gcd(A, B, GCD):-H is A mod B, gcd(B, H, GCD).

% gcd of a list of numbers
gcd([E], E).
gcd([H|T], GCD):-gcd(T, GCD_Tail), gcd(H, GCD_Tail, GCD).

?- read(L), gcd(L, GCD).
|: [744480, 1231776, 4372128].
L = [744480, 1231776, 4372128],
GCD = 13536 ;
false.