From 744b795bf1ff0f4438f7814fb1f1796a3ddf5798 Mon Sep 17 00:00:00 2001 From: "R. Tyler Croy" Date: Sun, 13 Mar 2011 14:25:35 -0700 Subject: [PATCH] Add solution to problem one A very nice solution if you ask me xD --- one.adb | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 one.adb diff --git a/one.adb b/one.adb new file mode 100644 index 0000000..55aabaf --- /dev/null +++ b/one.adb @@ -0,0 +1,55 @@ +-- +-- Project Euler problem #1 +-- + +with Ada.Containers.Vectors, + Ada.Command_Line, + Ada.Text_IO; + +use Ada.Containers, + Ada.Text_IO; + +procedure One is + package CL renames Ada.Command_Line; + package Bucket is new Ada.Containers.Vectors (Index_Type => Natural, + Element_Type => Natural); + -- + -- Print out multiples of 3 and 5 from 0 to the `Upper` + function Multiples (Upper : in Natural) return Bucket.Vector is + Numbers : Bucket.Vector; + begin + -- Non-inclusive + for Index in 1 .. (Upper - 1) loop + if (Index mod 3) = 0 then + Bucket.Append (Numbers, Index); + elsif (Index mod 5) = 0 then + Bucket.Append (Numbers, Index); + end if; + end loop; + return Numbers; + end Multiples; +begin + if CL.Argument_Count < 1 then + Put_Line (">>> Insufficient command line parameters!"); + New_Line; + Put_Line ("Please execute with: " & CL.Command_Name & " "); + CL.Set_Exit_Status (CL.Failure); + return; + end if; + + declare + Upper_Bound : constant Natural := Natural'Value (Ada.Command_Line.Argument (1)); + Valid_Numbers : constant Bucket.Vector := Multiples (Upper_Bound); + Result : Natural := 0; + begin + for Index in 0 .. Bucket.Last_Index (Valid_Numbers) loop + declare + Value : constant Natural := Bucket.Element (Valid_Numbers, Integer (Index)); + begin + Result := (Result + Value); + end; + end loop; + + Put_Line (Natural'Image (Result)); + end; +end One;