/five.adb

http://github.com/rtyler/euler-ada · Ada · 42 lines · 25 code · 6 blank · 11 comment · 4 complexity · 17002b9431f1578c43b234d730e5ce2a MD5 · raw file

  1. --
  2. -- Project Euler problem #5
  3. --
  4. -- 2520 is the smallest number that can be divided by each of the numbers from
  5. -- 1 to 10 without any remainder.
  6. --
  7. -- What is the smallest positive number that is evenly divisible by all of the
  8. -- numbers from 1 to 20?
  9. with Ada.Text_IO;
  10. use Ada.Text_IO;
  11. procedure Five is
  12. Valid_Numbers : array (1 .. 20) of Natural;
  13. -- Stepping up with the highest number we need to be a multiple of
  14. Step : constant Natural := Valid_Numbers'Last;
  15. Should_Continue : Boolean := true;
  16. Value : Natural := 0;
  17. -- Divisible_By_Set will return early with false as soon as it hits on a
  18. -- number that is not cleanly divisible (i.e. no remainer)
  19. function Divisible_By_Set (Number : in Natural) return Boolean is
  20. begin
  21. for Item in Valid_Numbers'Range loop
  22. if (Number mod Item) /= 0 then
  23. return false;
  24. end if;
  25. end loop;
  26. return true;
  27. end Divisible_By_Set;
  28. begin
  29. while Should_Continue loop
  30. Value := (Value + Step);
  31. if Divisible_By_Set (Value) then
  32. Should_Continue := false;
  33. end if;
  34. end loop;
  35. Put_Line (Natural'Image (Value));
  36. end Five;