/euler002/Euler002b.adb
Ada | 93 lines | 64 code | 9 blank | 20 comment | 5 complexity | 9bcdd11cc8226ced58fbeb89cb2800ad MD5 | raw file
- pragma License ( GPL );
- ------------------------------------------------------------------------------
- -- Author: darkestkhan --
- -- Email: darkestkhan@gmail.com --
- -- License: GNU GPLv3 or any later as published by Free Software Foundation --
- -- ( see license.txt file ) --
- -- Copyright  2011 darkestkhan --
- ------------------------------------------------------------------------------
- -- This Program is Free Software: You Can Redistribute It and/or Modify --
- -- It Under The Terms of The GNU General Public License As Published By --
- -- 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 Ada.Task_Identification;
- with TTY; -- it is at <https://github.com/darkestkhan/tty>
- with Ada.Command_Line;
- with Ada.IO_Exceptions;
- procedure Euler002b is
- Terminal: TTY.TTY;
- Argc: constant Integer := Ada.Command_Line.Argument_Count;
- 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 );
- task body Calculate is
- 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" );
- Ada.Task_Identification.Abort_Task ( Ada.Task_Identification.Current_Task );
- 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;
- task type Creator;
- task body Creator is
- Arg: Integer;
- begin
- if Argc < 1 then
- Terminal.Write_Line ( "Usage: " & Ada.Command_Line.Command_Name & " [int] ... " );
- Ada.Task_Identification.Abort_Task ( Ada.Task_Identification.Current_Task );
- 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." );
- Ada.Task_Identification.Abort_Task ( Ada.Task_Identification.Current_Task );
- end Creator;
- God: Creator;
- begin
- null;
- end Euler002b;