Project Euler #5: Smallest multiple

Sort by

recency

|

264 Discussions

|

  • + 0 comments

    Python 3:

    #!/bin/python3
    
    import sys
    
    def findGCD(a,b):
        while b!=0:
            remainder = a%b
            a=b
            b=remainder
            
        return a
        
    def lcmUsingGCD(a,b):
        return abs(a*b)//findGCD(a,b)
        
    def numLCM(n):
        lcm=1
        for i in range(2, n+1):
            lcm = lcmUsingGCD(lcm, i)
        print(lcm)
    
    t = int(input().strip())
    for a0 in range(t):
        n = int(input().strip())
        numLCM(n)
    
  • + 0 comments

    C++ code:

    #include <cmath>
    #include <cstdio>
    #include <iostream>
    using namespace std;
    
    int smallest_div(int n){
        int i=0;
        bool continuer(true);
        while(continuer){
            i++;
            continuer=false;
            for(int j=1;j<=n;j++){
                if (i%j != 0){
                   continuer=true;
                   continue;
                 }
             }
        }
        return i;
    }
    
    
    int main() {
        int t, n;
        cin >> t;
        for(int line=0;line < t; line++){
            cin >> n;
            cout << smallest_div(n) << endl;
        }  
        return 0;
    }
    
  • + 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));