Sum of LCM digits

What is the sum of digits of the LCM of first 30 natural numbers?


The answer is 54.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

Arulx Z
Aug 15, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
>>> def gcd (a, b):
        while b > 0:
            temp = b
            b = a % b
            a = temp
        return a
>>> def lcm (a, b):
        return a * (b / gcd (a, b))
>>> temp = 2
>>> for x in xrange (3, 31):
        temp = lcm (temp, x)
>>> sum(int(x) for x in str(temp))
54

Pranjal Jain
Mar 22, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from fractions import *
def lcm(x,y):
    k=(x*y)/gcd(x,y)
    return k
a=1
b=2
while(b<31):
    c=lcm(a,b)
    b+=1
    a=c
def digitalsum(x):
    """Returns sum of digits of the number"""
    if x<10:
        return x
    else:
        return (x%10)+digitalsum(x//10)
digitalsum(c)

Aryan Gaikwad
Mar 22, 2015

Java solution -

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public static void main(String args[]){
    double x = 1;
    for(double i = 1; i <= 30; i++)
        x = lcm(x, i);
    System.out.println(x); //prints the LCM
}

private static long lcm(long a, long b){
    return a * (b / gcd(a, b));
}

private static long gcd(long a, long b){
    while (b > 0) {
        long temp = b;
        b = a % b; // % is remainder
        a = temp;
    }
    return a;
}

Execution time ~ 0.0014 secs

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...