/seven.adb

http://github.com/rtyler/euler-ada · Ada · 43 lines · 27 code · 6 blank · 10 comment · 5 complexity · 643422ca0cbef34df4975dca06538c0f MD5 · raw file

  1. --
  2. -- Project Euler problem #7
  3. --
  4. -- By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see
  5. -- that the 6th prime is 13.
  6. --
  7. -- What is the 10001st prime number?
  8. --
  9. with Ada.Numerics.Elementary_Functions,
  10. Ada.Text_IO;
  11. use Ada.Numerics.Elementary_Functions,
  12. Ada.Text_IO;
  13. procedure Seven is
  14. function Is_Prime (Input : in Natural) return Boolean is
  15. -- Using the "Trial Division" method detailed in this Wikipedia entry:
  16. -- https://secure.wikimedia.org/wikipedia/en/wiki/Prime_number#Trial_division
  17. Ceiling : constant Natural := Natural (Sqrt (Float (Input)));
  18. begin
  19. for Index in 2 .. Ceiling loop
  20. if (Input mod Index) = 0 then
  21. return false;
  22. end if;
  23. end loop;
  24. return true;
  25. end Is_Prime;
  26. Count : Natural := 0;
  27. begin
  28. for N in 2 .. Natural'Last loop
  29. if Is_Prime (N) then
  30. Count := (Count + 1);
  31. if Count = 10_001 then
  32. Put_Line (Natural'Image (N) & " is prime");
  33. return;
  34. end if;
  35. end if;
  36. end loop;
  37. end Seven;