[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
QUICK WRITE A PROGRAM IN YOUR FAVORITE LANGUAGE THAT ASKS THE
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 62
Thread images: 13
File: killer bird.jpg (29 KB, 412x430) Image search: [Google]
killer bird.jpg
29 KB, 412x430
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
>>
>>55597105

Shit thread
>>
File: 1468102590495.jpg (51 KB, 540x489) Image search: [Google]
1468102590495.jpg
51 KB, 540x489
>>55597126
>>
>>55597105
Fuck off already
>>
BIRDMIN NO
>>
File: kiss.gif (479 KB, 300x374) Image search: [Google]
kiss.gif
479 KB, 300x374
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 <- putStr "type space separated list of integers: " >> 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 (~v & 1)
return gcd(u, v >> 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 anon");

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
>>
File: 1467682413689.png (715 KB, 791x936) Image search: [Google]
1467682413689.png
715 KB, 791x936
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.
>>
File: 1466464760984.png (593 KB, 1185x1029) Image search: [Google]
1466464760984.png
593 KB, 1185x1029
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
>>
File: 1468297993819.jpg (19 KB, 217x320) Image search: [Google]
1468297993819.jpg
19 KB, 217x320
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
>>
File: 1468188815921.gif (18 KB, 485x485) Image search: [Google]
1468188815921.gif
18 KB, 485x485
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*))))))
>>
>>55598656
>no function call

enjoy your library
>>
<?php
echo gmp_strval(gmp_gcd ( gmp_init($_GET['a']) , gmp_init($_GET['b'])));
?>
>>
>>55598840
Where is the user input you stupid frog? Do you want to get stabbed this badly?
>>
npm install common-divisor --save


I love JavaScript
>>
>>55599440
I have a library?
>>
>>55598326
>Pls no stab me furious bird.

Then get to work.
>>
>quick, do my homework

fuck off to /dpt/
>>
>>55597638
that is messy as fuck code
>>
>>55599697
>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
>>
>>55597105
>QUICK /g/ DO MY HOMEWORK OR LE BIRD WILL LE STABB U :DD

>you all fall for it

jesus christ
>>
>>55597105
gcd(A, 0) -> 
A;
gcd(A, B) ->
gcd(B, A rem B).
>>
>>55598279

I have literally no comprehension of what the fuck that code does.
>>
File: 1468326741525.jpg (170 KB, 528x960) Image search: [Google]
1468326741525.jpg
170 KB, 528x960
*goto behind you*
>>
>>55598840
best solution
>>
File: 1468263088722.gif (1 MB, 304x156) Image search: [Google]
1468263088722.gif
1 MB, 304x156
>>55601306
>>
>>55600501
These are never hard to code, shut the hell up
>>
>>55601661

>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))
>>
>>55597105

# 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])
>>
>>55597105
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)))
>>
>>55599500
It even works, best solution in the whole thread.
>>
>>55600239

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.
>>
>>55601848
nice.
>>
>>55597105
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<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
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<Integer> 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(ArrayList<ArrayList<Integer>>factorLists)
{
int smallestArrayIndex = 0;
for (int i=0; i<factorLists.size(); i++)
{if (smallestArrayIndex < (factorLists.get(i).size())){
smallestArrayIndex = i;
}
}

for (int i=factorLists.get(smallestArrayIndex).size(); i>0; i--)
{
Integer currentFactor = factorLists.get(smallestArrayIndex).get(i-1);
int gcfFoundCounter = factorLists.size();
for(int j=0; j<factorLists.size(); j++)
{
if (factorLists.get(j).contains(currentFactor))
{
gcfFoundCounter = gcfFoundCounter - 1;
if (gcfFoundCounter == 0){
return currentFactor;
}
}
}
}
int TheGCF = -1;
return TheGCF;
}
public static ArrayList<Integer> getFactors(int num)
{
ArrayList<Integer> factorArray = new ArrayList<Integer>();
for (int i = 1; i < num; i++) {
if (num % i == 0) {
factorArray.add(i);
}
}
return factorArray;
}
}
>>
File: le horrified mickey.jpg (114 KB, 615x1279) Image search: [Google]
le horrified mickey.jpg
114 KB, 615x1279
>>55604905
>>
>>55597105
# heuristics
print 1
>>
>>55597105
C++
void euclidean(int a, int b) {
int x = a / (double)b;
int y = a % b;

if (y != 0) {
euclidean(b, y);
}
else {
cout << "\nGCD(" << a << ',' << b << ") = " << b << endl;
}
}
>>
>>55605345
>INPUT A BUNCH OF INTEGERS
I can't read, disregard this post
>>
>>55604905
>boolean going = true;
> while (going == true){

Dude, one of us is getting stabbed and it's not me.
>>
#include <stdio.h>

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;
}
>>
File: disgusted spic.png (129 KB, 245x424) Image search: [Google]
disgusted spic.png
129 KB, 245x424
>>55604905
>int TheGCF = -1;
>return TheGCF;
>>
Why not Mathematica? Fill in an array called list and then the code is:

GCD[list]
>>
>>55605658
>>55603390
>>
>>55602069
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?
>>
>>55606994
You're hired.
>>
>>55606994
[c0de]

[/c0de]
>>
>>55606994
write #code in the options field
>>
>>55597111
So when is that while True loop going to break so that you can actually print the gcf?
>>
File: 1463523736822.jpg (103 KB, 500x625) Image search: [Google]
1463523736822.jpg
103 KB, 500x625
>>5560528
This
>>
>>55600528
Its ok, just compile it and execute it. stope asking questions.
>>
File: abap.png (16 KB, 700x600) Image search: [Google]
abap.png
16 KB, 700x600
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.
Thread replies: 62
Thread images: 13

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.