/euler002/Euler002.adb
Ada | 69 lines | 43 code | 6 blank | 20 comment | 5 complexity | c114401773936eccff612b6f464435ec 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 README 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.Text_IO;
- with Ada.Command_Line;
- with Ada.IO_Exceptions;
- procedure Euler002 is
- 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;
- Arg: Integer;
- Sum: Integer;
- begin
- if Argc = 0 then
- Ada.Text_IO.Put_Line ( "Usage: " & Ada.Command_Line.Command_Name & " [int] ... " );
- else
- for Index in 1 .. Argc loop
- Arg := Integer'Value ( Ada.Command_Line.Argument ( Index );
- Sum := Sum_Even_Fib ( Limit => Arg );
- if Sum = -1 then
- Ada.Text_IO.Put_Line ( "The sum of even terms of Fibonacci series below " & Arg'img & " is greater than integer'last" );
- else
- Ada.Text_IO.Put_Line ( "The sum of even terms of Fibonacii series below " & Arg'img & " equals " & Sum'img );
- end if;
- end loop;
- end if;
- exception
- when Ada.IO_Exceptions.Data_Error => Ada.Text_IO.Put_Line ( "One of arguments was too big. Aborting. Aborting." );
- end Euler002;