PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 53ms app.codeStats 1ms

/euler002/Euler002.adb

http://github.com/darkestkhan/project_euler
Ada | 69 lines | 43 code | 6 blank | 20 comment | 5 complexity | c114401773936eccff612b6f464435ec MD5 | raw file
 1pragma License ( GPL );
 2------------------------------------------------------------------------------
 3-- Author: darkestkhan                                                      --
 4-- Email:  darkestkhan@gmail.com                                            --
 5-- License: GNU GPLv3 or any later as published by Free Software Foundation --
 6-- ( see README file )                                                      --
 7-- Copyright Š 2011 darkestkhan                                             --
 8------------------------------------------------------------------------------
 9-- This Program is Free Software: You Can Redistribute It and/or Modify     --
10-- It Under The Terms of The GNU General Public License As Published By     --
11-- The Free Software Foundation, Either Version 3 of The License, or        --
12-- (at Your Option) Any Later Version.                                      --
13--                                                                          --
14-- This Program is Distributed in The Hope That It Will Be Useful,          --
15-- But WITHOUT ANY WARRANTY; Without Even The Implied Warranty of           --
16-- MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE.  See The            --
17-- GNU General Public License for More Details.                             --
18--                                                                          --
19-- You Should Have Received A Copy of The GNU General Public License        --
20-- Along with This Program.  if not, See <Http://Www.Gnu.Org/Licenses/>.    --
21------------------------------------------------------------------------------
22with Ada.Text_IO;
23with Ada.Command_Line;
24with Ada.IO_Exceptions;
25procedure Euler002 is
26
27  Argc: constant Integer := Ada.Command_Line.Argument_Count;
28
29  function Sum_Even_Fib ( Limit: in Integer ) return Integer is
30    Sum: Integer := 0;
31    First: Integer := 1;
32    Second: Integer := 2;
33    Fib: Integer := Second;
34  begin
35    while Fib < Limit loop
36      if Fib mod 2 = 0 then
37        Sum := Sum + Fib;
38      end if;
39      Fib := First + Second;
40      First := Second;
41      Second := Fib;
42    end loop;
43    return Sum;
44  exception
45    when Constraint_Error => return -1;
46  end Sum_Even_Fib;
47
48  Arg: Integer;
49  Sum: Integer;
50
51begin
52
53  if Argc = 0 then
54    Ada.Text_IO.Put_Line ( "Usage: " & Ada.Command_Line.Command_Name & " [int] ... " );
55  else
56    for Index in 1 .. Argc loop
57      Arg := Integer'Value ( Ada.Command_Line.Argument ( Index );
58      Sum := Sum_Even_Fib ( Limit => Arg );
59      if Sum = -1 then
60        Ada.Text_IO.Put_Line ( "The sum of even terms of Fibonacci series below " & Arg'img  & " is greater than integer'last" );
61      else
62        Ada.Text_IO.Put_Line ( "The sum of even terms of Fibonacii series below " & Arg'img & " equals " & Sum'img );
63      end if;
64    end loop;
65  end if;
66
67exception
68  when Ada.IO_Exceptions.Data_Error => Ada.Text_IO.Put_Line ( "One of arguments was too big. Aborting. Aborting." );
69end Euler002;