We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
John and GCD list
John and GCD list
Sort by
recency
|
30 Discussions
|
Please Login in order to post a comment
C solution
def gcd(a,b):
if b==0 :
def ppcm(a,b): return a*b//gcd(a,b)
def solve(a): # Write your code here L=[a[0]] for i in range(len(a)-1): L.append(ppcm(a[i],a[i+1])) L.append(a[-1]) return L
include
int gcd(int,int); int lcm(int,int); int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int lcm(int a, int b) { return (a*b)/gcd(a,b); } int main() { int t; scanf("%d",&t); while(t--) { int n,i; scanf("%d",&n); int a[n]; for(i=0 ; ia[i]) printf("%d ",lcm(a[i+1],a[i])); else printf("%d ",lcm(a[i],a[i+1])); } printf("%d\n",a[n-1]);
} return 0; }
C# - the only problem, calculate min Multiple for 2 numbers
...not the shortest code though
Some hint:
Suppose the input is [A,B,C,,...]
The first value of the list should be one number X such that : A = gcd(X,??) In other words: X should be multiple of A.
Because the sum have to be minimal, lets try the lowest multiple of A (X = A).
From here, whats the second number Y of the list? We have two restrictions of Y: A = gcd(X,Y) B = gcd(Y, ??) In other words Y should be a multiple of A and a multiple of B. Again, because the sum have to be minimal, get the lowest multiple of A and B ( Least Common Multiple)
Iterate from there...