PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/euler002/Euler002.adb

http://github.com/darkestkhan/project_euler
Ada | 69 lines | 43 code | 6 blank | 20 comment | 5 complexity | c114401773936eccff612b6f464435ec MD5 | raw file
  1. pragma 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. ------------------------------------------------------------------------------
  22. with Ada.Text_IO;
  23. with Ada.Command_Line;
  24. with Ada.IO_Exceptions;
  25. procedure Euler002 is
  26. Argc: constant Integer := Ada.Command_Line.Argument_Count;
  27. function Sum_Even_Fib ( Limit: in Integer ) return Integer is
  28. Sum: Integer := 0;
  29. First: Integer := 1;
  30. Second: Integer := 2;
  31. Fib: Integer := Second;
  32. begin
  33. while Fib < Limit loop
  34. if Fib mod 2 = 0 then
  35. Sum := Sum + Fib;
  36. end if;
  37. Fib := First + Second;
  38. First := Second;
  39. Second := Fib;
  40. end loop;
  41. return Sum;
  42. exception
  43. when Constraint_Error => return -1;
  44. end Sum_Even_Fib;
  45. Arg: Integer;
  46. Sum: Integer;
  47. begin
  48. if Argc = 0 then
  49. Ada.Text_IO.Put_Line ( "Usage: " & Ada.Command_Line.Command_Name & " [int] ... " );
  50. else
  51. for Index in 1 .. Argc loop
  52. Arg := Integer'Value ( Ada.Command_Line.Argument ( Index );
  53. Sum := Sum_Even_Fib ( Limit => Arg );
  54. if Sum = -1 then
  55. Ada.Text_IO.Put_Line ( "The sum of even terms of Fibonacci series below " & Arg'img & " is greater than integer'last" );
  56. else
  57. Ada.Text_IO.Put_Line ( "The sum of even terms of Fibonacii series below " & Arg'img & " equals " & Sum'img );
  58. end if;
  59. end loop;
  60. end if;
  61. exception
  62. when Ada.IO_Exceptions.Data_Error => Ada.Text_IO.Put_Line ( "One of arguments was too big. Aborting. Aborting." );
  63. end Euler002;