PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/euler001/Euler001b.adb

http://github.com/darkestkhan/project_euler
Ada | 99 lines | 66 code | 12 blank | 21 comment | 6 complexity | 8d52b2a2265bc8c94d82296182e1c0e2 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.Command_Line;
  23. with Ada.Task_Identification;
  24. with Ada.Text_IO;
  25. with TTY; -- it is in <https://github.com/darkestkhan/tty>
  26. with Ada.IO_Exceptions;
  27. procedure Euler001b is
  28. Argc: constant Integer := Ada.Command_Line.Argument_Count;
  29. Terminal: TTY.TTY;
  30. task type Calculate ( Up_To: Integer );
  31. type Calculate_Array is array ( Positive range <> ) of access Calculate;
  32. function Sum_Up_To ( Limit: in Integer ) return Integer is
  33. Sum: Natural := 0;
  34. I: Integer := 0;
  35. begin
  36. while I < Limit loop
  37. Sum := Sum + i;
  38. I := I + 3;
  39. end loop;
  40. I := 5;
  41. while I < Limit loop
  42. if I mod 3 /= 0 then
  43. Sum := Sum + I;
  44. end if;
  45. I := I + 5;
  46. end loop;
  47. return Sum;
  48. end Sum_Up_To;
  49. task body Calculate is
  50. Sum: Integer;
  51. begin
  52. Sum := Sum_Up_To ( Limit => Up_To );
  53. Terminal.Write_Line ( "The sum of numbers divisible by 3 or 5, but less than " & Up_To'img & " is " & Sum'img );
  54. Ada.Task_Identification.Abort_Task ( Ada.Task_Identification.Current_Task );
  55. exception
  56. when Constraint_Error => Terminal.Write ( "The sum in case of " & Up_To'img & " is greater than integer range" );
  57. Ada.Task_Identification.Abort_Task ( Ada.Task_Identification.Current_Task );
  58. end Calculate;
  59. function Create ( Up_To: Integer ) return access Calculate is
  60. begin
  61. return new Calculate ( Up_To => Up_To );
  62. end Create;
  63. task type Creator;
  64. task body Creator is
  65. Limit: Integer;
  66. Calculators: Calculate_Array ( 1 .. Argc );
  67. begin
  68. if Argc = 0 then
  69. Terminal.Write ( "Usage: " & Ada.Command_Line.Command_Name & " int [int] [int] ... " );
  70. Ada.Task_Identification.Abort_Task ( Ada.Task_Identification.Current_Task );
  71. end if;
  72. for Index in 1 .. Argc loop
  73. Integer'Value ( Ada.Command_Line.Argument ( Index ) );
  74. -- NOTE if given too big argument will raise Ada.IO.Exceptions.Data_Error;
  75. if Limit < 1 then
  76. Terminal.Write ( "I can sum only numbers greater than 0 and you gave " & Limit'img );
  77. else
  78. Calculators ( Index ) := Create ( Up_To => Limit );
  79. end if;
  80. end loop;
  81. exception
  82. when Ada.IO_Exceptions.Data_Error => Ada.Text_IO.Put_Line ( "One of given arguments was too big. Aborting. Aborting." );
  83. end Creator;
  84. God: Creator;
  85. begin
  86. null;
  87. end Euler001b;