Project Euler #5: Smallest multiple

Sort by

recency

|

262 Discussions

|

  • + 1 comment
    s=1
        for p in range(1,n+1):
            if math.lcm(p,s)!=s: s=math.lcm(p,s)
        print(s)
    
  • + 0 comments

    JAVA SOLUTION

    import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

    public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int a0 = 0; a0 < t; a0++){
            int n = in.nextInt();
    
            System.out.println(check(n));
        }
    }
    
    public static long check(int n)
    {
    
        long a=1;
    
        //finding the lcm of all numbers till n to get answer
        for(long b=2;b<=n;b++)
        {            
            a= lcm(b,a);
        }
    
        return a;
    
    
    }
    
    public static long lcm(long a,long b)
    {
    
        long z=a<b?a:b;
    
        while(true)
        {
            if(z%a==0 && z%b==0)
            return z;
            z++;
        }
    }
    

    }

  • + 0 comments

    function gcd(a, b) { while (b !== 0) { let temp = b; b = a % b; a = temp; } return a; }

    function lcm(a, b) { return (a * b) / gcd(a, b); }

    function smallestMultiple(n) { let result = 1; for (let i = 2; i <= n; i++) { result = lcm(result, i); } return result; }

    let n = parseInt(readLine()); console.log(smallestMultiple(n));

  • + 0 comments

    import math from functools import reduce

    def lcm(a, b): return abs(a*b) // math.gcd(a, b)

    def lcm_of_range(n): return reduce(lcm, range(1, n+1))

    Input handling

    t = int(input().strip()) for _ in range(t): n = int(input().strip()) result = lcm_of_range(n) print(result)

  • + 0 comments
    t = int(input().strip())
    for a0 in range(t):
        n = int(input().strip())
        should_con = False
        ans = 0
        for num in range(n, n ** 5 + 1):
            for i in range(1, n + 1):
                if num % i == 0:
                    should_con = True
                else:
                    should_con = False
                    break
            if should_con:
                ans = num
                break
        print(ans)