PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/euler001/Euler001.adb

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