PageRenderTime 1ms CodeModel.GetById 156ms app.highlight 63ms RepoModel.GetById 139ms app.codeStats 1ms

/euler001/Euler001.adb

http://github.com/darkestkhan/project_euler
Ada | 60 lines | 33 code | 6 blank | 21 comment | 4 complexity | 84c36d9680e1603b186f5d974e60f314 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.Text_IO;
23with Ada.Command_Line;
24with Ada.IO_Exceptions;
25procedure Euler001 is
26
27  function Sum_Up_To ( Limit: in Integer ) return Integer is
28    Sum: Natural := 0;
29    I: Integer := 3;
30  begin
31    while I < Limit loop
32      Sum := Sum + I;
33      I := I + 3;
34    end loop;
35
36    I := 5;
37    while I < Limit loop
38      if I mod 3 /= 0 then
39        Sum := Sum + I;
40      end if;
41      I := I + 5;
42    end loop;
43    return Sum;
44  end Sum_Up_To;
45
46  Sum: Integer;
47  Limit: Integer;
48
49begin
50
51  for Index in 1 .. Ada.Command_Line.Argument_Count loop
52    Limit := Integer'Value ( Ada.Command_Line.Argument ( Index ) );
53    --NOTE if argument is greater than integer'range then data_error will be raised
54    Sum := Sum_Up_To ( Limit => Limit  );
55    Ada.Text_IO.Put_Line ( "The sum of numbers divisible by 3 or 5, but less than " & Ada.Command_Line.Argument ( Index ) & " is " & Sum'img );
56  end loop;
57exception
58  when Ada.IO_Exceptions.Data_Error => Ada.Text_IO.Put_Line ( "One of arguments was too big. Aborting. Aborting." );
59end Euler001;
60