Smallest Multiple
Problem 5 Project Euler
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) { int t = Convert.ToInt32(Console.ReadLine()); for(int a0 = 0; a0 < t; a0++){ int n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(SmallestNumber(n)); } } static int SmallestNumber(int n) { var m=1; while(true) { if(Divisible(n,m)) return m; m++; } } static bool Divisible(int n,int m) { for(int i=1; i<=n;i++) { if(m%i!=0) return false; } return true; } }
Live Code on IDE One
Comments
Post a Comment