Advertisement
Guest User

Untitled

a guest
Oct 15th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.75 KB | None | 0 0
  1. import std.stdio;
  2.  
  3. bool divisibleBy1to20(immutable int x) pure nothrow @safe
  4. {
  5.     // We already know that this number is divisible by 20 and everything under 11
  6.     const nums = [11,12,13,14,15,16,17,18,19];
  7.     foreach(int i; nums)
  8.     {
  9.         if(x % i != 0)
  10.             return false;
  11.     }
  12.     return true;
  13. }
  14.  
  15. unittest
  16. {
  17.     assert(divisibleBy1to20(20) == false);
  18.     assert(divisibleBy1to20(19) == false);
  19.     assert(divisibleBy1to20(1) == false);
  20.     assert(divisibleBy1to20(-1) == false);
  21.     assert(divisibleBy1to20(-20) == false);
  22.     assert(divisibleBy1to20(232792560) == true);
  23. }
  24.  
  25. int main()
  26. {
  27.     int i = 20;
  28.     while(!divisibleBy1to20(i))
  29.     {
  30.         i=i+20;
  31.     }
  32.     writefln("The smallest positive number that is evenly divisible by all of the numbers from 1 to 20 is %d.",i);
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement