Skip to content
Newer
Older
100644 40 lines (35 sloc) 1.14 KB
8c101ed @rtyler Add solution for problem ten
authored
1 --
2 -- Project Euler problem #10
3 --
4 -- The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
5 --
6 -- Find the sum of all the primes below two million.
7 --
8
9 with Ada.Numerics.Elementary_Functions,
10 Ada.Text_IO;
11
12 use Ada.Numerics.Elementary_Functions,
13 Ada.Text_IO;
14
15 procedure Ten is
16 subtype Long_Natural is Long_Integer range 0 .. Long_Integer'Last;
17 Ceiling : constant Natural := 2_000_000;
18 Result : Long_Natural := 0;
19
20 function Is_Prime (Input : in Natural) return Boolean is
21 -- Using the "Trial Division" method detailed in this Wikipedia entry:
22 -- https://secure.wikimedia.org/wikipedia/en/wiki/Prime_number#Trial_division
23 Boundary : constant Natural := Natural (Sqrt (Float (Input)));
24 begin
25 for Index in 2 .. Boundary loop
26 if (Input mod Index) = 0 then
27 return false;
28 end if;
29 end loop;
30 return true;
31 end Is_Prime;
32 begin
33 for Index in 2 .. Ceiling loop
34 if Is_Prime (Index) then
35 Result := (Result + Long_Natural (Index));
36 end if;
37 end loop;
38 Put_Line ("Sum of all primes:" & Long_Natural'Image (Result));
39 end Ten;
Something went wrong with that request. Please try again.