PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/euler002/Euler002b.adb

http://github.com/darkestkhan/project_euler
Ada | 93 lines | 64 code | 9 blank | 20 comment | 5 complexity | 9bcdd11cc8226ced58fbeb89cb2800ad 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 license.txt 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.Task_Identification;
  23. with TTY; -- it is at <https://github.com/darkestkhan/tty>
  24. with Ada.Command_Line;
  25. with Ada.IO_Exceptions;
  26. procedure Euler002b is
  27. Terminal: TTY.TTY;
  28. Argc: constant Integer := Ada.Command_Line.Argument_Count;
  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. task type Calculate ( Arg: Integer );
  48. task body Calculate is
  49. Sum: Integer;
  50. begin
  51. Sum := Sum_Even_Fib ( Limit => Arg );
  52. if Sum = -1 then
  53. Terminal.Write_Line ( "The sum of even terms of Fibonacci series below " & Arg'img & " is greater than integer'last" );
  54. Ada.Task_Identification.Abort_Task ( Ada.Task_Identification.Current_Task );
  55. else
  56. Terminal.Write_Line ( "The sum of even terms of Fibonacii series below " & Arg'img & " equals " & Sum'img );
  57. end if;
  58. end Calculate;
  59. type Calculate_Array is array ( Integer range <> ) of access Calculate;
  60. Calculates: Calculate_Array ( 1 .. Argc );
  61. function Create ( Arg: in Integer ) return access Calculate is
  62. begin
  63. return new Calculate ( Arg => Arg );
  64. end Create;
  65. task type Creator;
  66. task body Creator is
  67. Arg: Integer;
  68. begin
  69. if Argc < 1 then
  70. Terminal.Write_Line ( "Usage: " & Ada.Command_Line.Command_Name & " [int] ... " );
  71. Ada.Task_Identification.Abort_Task ( Ada.Task_Identification.Current_Task );
  72. end if;
  73. for Index in 1 .. Argc loop
  74. Arg := Integer'Value ( Ada.Command_Line.Argument ( Index );
  75. Calculates ( Index ) := Create ( Arg => Arg );
  76. end loop;
  77. exception
  78. when Ada.IO_Exceptions.Data_Error => Terminal.Write_Line ( "One of arguments was too big. Aborting. Aborting." );
  79. Ada.Task_Identification.Abort_Task ( Ada.Task_Identification.Current_Task );
  80. end Creator;
  81. God: Creator;
  82. begin
  83. null;
  84. end Euler002b;