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

QUICK WRITE A PROGRAM THAT CALCULATES THE GREATEST COMMON DIVISOR

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 15
Thread images: 5

File: 1484332469273.jpg (32KB, 412x430px) Image search: [Google]
1484332469273.jpg
32KB, 412x430px
QUICK

WRITE A PROGRAM THAT CALCULATES THE GREATEST COMMON DIVISOR OF A LIST OF NUMBERS OR THIS BIRD IS GONNA STAB YOU
>>
#include <stdio.h>
#include <stdlib.h>

#define CAWCAWMOTHERFUCKERS
#define SIZE(x) ((sizeof x) / (sizeof *x))

int gcd(int a,int b) {
int x;

while(b > 0) {
x = b;
b = a % b;
a = x;
}

return a;
}

int main(void) {
int numbers[] = {6,12,9,27};
int result = numbers[0];
int n;

for(n = 1; n < SIZE(numbers); n++) {
result = gcd(result,numbers[n]);
}

printf("%d\n",result);

return EXIT_SUCCESS;
}
>>
File: 1490530848674.gif (8KB, 69x61px) Image search: [Google]
1490530848674.gif
8KB, 69x61px
what all numbers? what is greatest divisor?
>>
>>59706177
I bet there's a function in PHP for that
>>
File: crow.jpg (31KB, 615x409px) Image search: [Google]
crow.jpg
31KB, 615x409px
>>
>>59706405
(defn gcd [nums] (reduce #(if (= %2 0) %1 (recur %2 (mod %1 %2))) nums))
>>
>>59706177
gcd = lambda a, b: gcd(b, a % b) if b else a
print reduce(lambda a, b: gcd(a, b), map(int, __import__("sys").argv[1:]))
>>
GET TO FUCKING WORK
>>
>>59706177
#include <iostream>
#include <vector>
#include <algorithm>


long int gcd (long int a, long int b)
{
while (a!=b)
{
if (a>b)
a-=b;
else b-=a;
}
return a;
}

long int gcd_vector (int il, int sl, std::vector <long int> A)
{
int middle{};
if (il==sl)
return A[sl];
else
{
middle=(il+sl)/2;
return gcd (gcd_vector(il,middle,A),gcd_vector(middle+1,sl,A));
}
}

int main()
{
std::vector <long int> elements {};
int n{};
long int number{};

do
{
std::cout<<"How many elements ?\n";
std::cin>>n;
}
while (n<=1);

for (int i=0; i<n; ++i)
{
std::cout<<"Element["<<i<<"]=";
std::cin>>number;
elements.push_back(number);
}

std::sort(elements.begin(),elements.end());

std::cout<<"The greatest common divisor of your list is:"<<gcd_vector(0,n-1,elements);

return 0;
}
>>
>>59707273
//fixed version
#include <iostream>
#include <vector>
#include <algorithm>


long int gcd (long int a, long int b)
{
while (a!=b)
{
if (a>b)
a-=b;
else b-=a;
}
return a;
}

long int gcd_vector (int il, int sl, std::vector <long int> A)
{
int middle{};

if (il==sl)
return A[sl];
else
{
middle=(il+sl)/2;
return gcd (gcd_vector(il,middle,A),gcd_vector(middle+1,sl,A));
}
}

int main()
{
std::vector <long int> elements {};
int n{};
long int number{};

do
{
std::cout<<"How many elements ?\n";
std::cin>>n;
}
while (n<=1);

for (int i=0; i<n; ++i)
{
do
{
std::cout<<"Element["<<i<<"]=";
std::cin>>number;
}
while (number<=0);//no negative
elements.push_back(number);
}

std::sort(elements.begin(),elements.end());

std::cout<<"The greatest common divisor of your list is:"<<gcd_vector(0,n-1,elements);

return 0;
}
>>
File: cawww_bitch.jpg (318KB, 1400x832px) Image search: [Google]
cawww_bitch.jpg
318KB, 1400x832px
>>59706177
from math import sqrt
def findHighestCommonFactor(listIn):
minVal=min(listIn)


factorsLow=[]
factorsHigh=[]
if minVal%2:
inc=2
else:
inc=1
for x in xrange(1, int(sqrt(minVal))+1, inc):
if minVal%x==0:
factorsLow.append(x)
factorsHigh.append(minVal/x)
factorsLow.reverse()
factors=factorsHigh+factorsLow

for factor in factors:
good=True
for val in listIn:
if val%factor:
good=False
break
if good:
return factor

testNums=[12,9,24,33]
print findHighestCommonFactor(testNums)
>>
>>59706613
damn
>>
def d(n) (1..n).select {|m| n % m == 0} end;r=d(ARGV[0].to_i);for n in 1..ARGV.length-1;r=r&d(ARGV[n].to_i);end;puts r.max;
>>
>>59706177
i'm too dumb for that
>>
>define æ = gcd(a,b)
>loop through æ_i = gcd(æ_i,a_i+1) for 0< i< length(list), whence æ_1 = gcd(a1,a2)
>if æ_i = 1 for any i then return 1
>else return æ_length(list)-1
>???
>benis
Thread posts: 15
Thread images: 5


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

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
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.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.