Project Euler #5: Smallest multiple

Sort by

recency

|

265 Discussions

|

  • + 0 comments

    Haskell

    import Control.Applicative
    import Control.Monad
    import System.IO
    
    c_gcd :: Int -> Int -> Int
    c_gcd a 0 = a
    c_gcd 0 b = b
    c_gcd a b 
        | a > b = c_gcd b (a `mod` b)
        | a < b = c_gcd a (b `mod` a)
    
    c_lcm :: Int -> Int -> Int
    c_lcm a b = (a * b) `div` (c_gcd a b)
    
    llcm :: [Int] -> Int
    llcm [x] = x
    llcm (x:xs) = c_lcm x (llcm xs)
    
    main :: IO ()
    main = do
        t_temp <- getLine
        let t = read t_temp :: Int
        forM_ [1..t] $ \a0  -> do
            n_temp <- getLine
            let n = read n_temp :: Int
            let n_list = [1..n]
            print (llcm n_list)
    
  • + 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++;
        }
    }
    

    }