Add solution for problem ten

This commit is contained in:
R. Tyler Croy 2011-03-14 20:44:51 -07:00
parent 3bcf86dfb0
commit 8c101ed8f6
1 changed files with 39 additions and 0 deletions

39
ten.adb Normal file
View File

@ -0,0 +1,39 @@
--
-- Project Euler problem #10
--
-- The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
--
-- Find the sum of all the primes below two million.
--
with Ada.Numerics.Elementary_Functions,
Ada.Text_IO;
use Ada.Numerics.Elementary_Functions,
Ada.Text_IO;
procedure Ten is
subtype Long_Natural is Long_Integer range 0 .. Long_Integer'Last;
Ceiling : constant Natural := 2_000_000;
Result : Long_Natural := 0;
function Is_Prime (Input : in Natural) return Boolean is
-- Using the "Trial Division" method detailed in this Wikipedia entry:
-- https://secure.wikimedia.org/wikipedia/en/wiki/Prime_number#Trial_division
Boundary : constant Natural := Natural (Sqrt (Float (Input)));
begin
for Index in 2 .. Boundary loop
if (Input mod Index) = 0 then
return false;
end if;
end loop;
return true;
end Is_Prime;
begin
for Index in 2 .. Ceiling loop
if Is_Prime (Index) then
Result := (Result + Long_Natural (Index));
end if;
end loop;
Put_Line ("Sum of all primes:" & Long_Natural'Image (Result));
end Ten;