Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- bool divisibleBy1to20(immutable int x) pure nothrow @safe
- {
- // We already know that this number is divisible by 20 and everything under 11
- const nums = [11,12,13,14,15,16,17,18,19];
- foreach(int i; nums)
- {
- if(x % i != 0)
- return false;
- }
- return true;
- }
- unittest
- {
- assert(divisibleBy1to20(20) == false);
- assert(divisibleBy1to20(19) == false);
- assert(divisibleBy1to20(1) == false);
- assert(divisibleBy1to20(-1) == false);
- assert(divisibleBy1to20(-20) == false);
- assert(divisibleBy1to20(232792560) == true);
- }
- int main()
- {
- int i = 20;
- while(!divisibleBy1to20(i))
- {
- i=i+20;
- }
- writefln("The smallest positive number that is evenly divisible by all of the numbers from 1 to 20 is %d.",i);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement