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.
Project Euler #5: Smallest multiple
Project Euler #5: Smallest multiple
Sort by
recency
|
263 Discussions
|
Please Login in order to post a comment
C++ code:
JAVA SOLUTION
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
public class Solution {
}
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));
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)