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