http://github.com/darkestkhan/project_euler
Ada | 93 lines | 64 code | 9 blank | 20 comment | 5 complexity | 9bcdd11cc8226ced58fbeb89cb2800ad MD5 | raw file
``````
------------------------------------------------------------------------------
-- Author: darkestkhan                                                      --
-- Email:  darkestkhan@gmail.com                                            --
-- ( see license.txt file )                                                 --
-- Copyright Â 2011 darkestkhan                                             --
------------------------------------------------------------------------------
-- This Program is Free Software: You Can Redistribute It and/or Modify     --
-- The Free Software Foundation, Either Version 3 of The License, or        --
-- (at Your Option) Any Later Version.                                      --
--                                                                          --
-- This Program is Distributed in The Hope That It Will Be Useful,          --
-- But WITHOUT ANY WARRANTY; Without Even The Implied Warranty of           --
-- MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE.  See The            --
-- GNU General Public License for More Details.                             --
--                                                                          --
-- You Should Have Received A Copy of The GNU General Public License        --
-- Along with This Program.  if not, See <Http://Www.Gnu.Org/Licenses/>.    --
------------------------------------------------------------------------------
with TTY; -- it is at <https://github.com/darkestkhan/tty>
procedure Euler002b is

Terminal: TTY.TTY;

function Sum_Even_Fib ( Limit: in Integer ) return Integer is
Sum: Integer := 0;
First: Integer := 1;
Second: Integer := 2;
Fib: Integer := Second;
begin
while Fib < Limit loop
if Fib mod 2 = 0 then
Sum := Sum + Fib;
end if;
Fib := First + Second;
First := Second;
Second := Fib;
end loop;
return Sum;
exception
when constraint_error => return -1;
end Sum_Even_Fib;

task type Calculate ( Arg: Integer );
Sum: Integer;
begin
Sum := Sum_Even_Fib ( Limit => Arg );
if Sum = -1 then
Terminal.Write_Line ( "The sum of even terms of Fibonacci series below " & Arg'img  & " is greater than integer'last" );
else
Terminal.Write_Line ( "The sum of even terms of Fibonacii series below " & Arg'img & " equals " & Sum'img );
end if;
end Calculate;

type Calculate_Array is array ( Integer range <> ) of access Calculate;
Calculates: Calculate_Array ( 1 .. Argc );

function Create ( Arg: in Integer ) return access Calculate is
begin
return new Calculate ( Arg => Arg );
end Create;

Arg: Integer;
begin
if Argc < 1 then
Terminal.Write_Line ( "Usage: " & Ada.Command_Line.Command_Name & " [int] ... " );
end if;

for Index in 1 .. Argc loop
Arg := Integer'Value ( Ada.Command_Line.Argument ( Index );
Calculates ( Index ) := Create ( Arg => Arg );
end loop;
exception
when Ada.IO_Exceptions.Data_Error => Terminal.Write_Line ( "One of arguments was too big. Aborting. Aborting." );