/euler003/Euler003.adb
Ada | 101 lines | 65 code | 7 blank | 29 comment | 9 complexity | 65ba66094cb60652a622d7ac3fa0ecfa MD5 | raw file
- pragma License ( GPL );
- ------------------------------------------------------------------------------
- -- Author: darkestkhan --
- -- Email: darkestkhan@gmail.com --
- -- License: GNU GPLv3 or any later as published by Free Software Foundation --
- -- ( see license.txt file ) --
- -- Copyright  2011 darkestkhan --
- ------------------------------------------------------------------------------
- -- This Program is Free Software: You Can Redistribute It and/or Modify --
- -- It Under The Terms of The GNU General Public License As Published By --
- -- The Free Software Foundation, Either Version 3 of The License, or --
- -- (at Your Option) Any Later Version. --
- -- --
- -- This Program is Distributed in The Hope That It Will Be Useful, --
- -- But WITHOUT ANY WARRANTY; Without Even The Implied Warranty of --
- -- MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE. See The --
- -- GNU General Public License for More Details. --
- -- --
- -- You Should Have Received A Copy of The GNU General Public License --
- -- Along with This Program. if not, See <Http://Www.Gnu.Org/Licenses/>. --
- ------------------------------------------------------------------------------
- with TTY;
- with Ada.Numerics.Long_Elementary_Functions;
- with Ada.Command_Line;
- with Ada.Task_Identification;
- with Ada.IO_Exceptions;
- procedure Euler003 is
- Argc: constant Integer := Ada.Command_Line.Argument_Count;
- Terminal: TTY.TTY;
- task type Largest_Factor ( To_Factorize: Long_Integer );
- task body Largest_Factor is
- Largest_Found: Long_Integer := 1;
- Search_Bound: constant Long_Integer := Long_Integer ( Ada.Numerics.Long_Elementary_Functions.Sqrt ( Long_Float ( To_Factorize ) ) ) + 1;
- -- conversion from float to long_integer may cause rounding error, in which case number that would be properly factored won't be ( consider 169 )
- Divisor: Long_Integer := 3;
- Number: Long_Integer := To_Factorize;
- begin
- if Number mod 2 = 0 then
- Largest_Found := 2;
- end if;
- while Number mod 2 = 0 loop
- Number := Number / 2;
- end loop;
- -- check for divisibility by 2
- while Divisor < Search_Bound and then Number /= 1 loop
- if Number mod Divisor = 0 then
- if Divisor > Largest_Found then
- Largest_Found := Divisor;
- end if;
- -- factor may be bigger than the one previously found
- while Number mod Divisor = 0 loop
- Number := Number / Divisor;
- end loop;
- -- number may be factored even many times by given factor, not factoring may result in false result ( consider 27 )
- end if;
- -- number was checked given ( possible ) factor so move on next
- Divisor := Divisor + 2;
- end loop;
- -- factorize ( trial division )
- if Number > Largest_Found then
- Largest_Found := Number;
- end if;
- -- it may be possible that to_factorize is prime number in which case it itself is the largest factor ( consider 13 )
- -- it may be also possible that it was coprime ( consider 26 )
- Terminal.Write_Line ( "Largest factor of " & To_Factorize'img & " is " & Largest_Found'img );
- Ada.Task_Identification.Abort_Task ( Ada.Task_Identification.Current_Task );
- end Largest_Factor;
- function Create ( To_Factorize: in Long_Integer ) return access Largest_Factor is
- begin
- return new Largest_Factor ( To_Factorize => To_Factorize );
- end Create;
- type Factorizing_Array is array ( Integer range <> ) of access Largest_Factor;
- Factorizers: Factorizing_Array ( 1 .. Argc );
- task type Creator;
- task body Creator is
- To_Factorize: Long_Integer;
- begin
- if Argc = 0 then
- Terminal.Write_Line ( "Usage: " & Ada.Command_Line.Command_Name & " [int] ... " );
- Ada.Task_Identification.Abort_Task ( Ada.Task_Identification.Current_Task );
- else
- for Index in 1 .. Argc loop
- To_Factorize := Long_Integer'Value ( Ada.Command_Line.Argument ( Index ) );
- Factorizers ( Index ) := Create ( To_Factorize => To_Factorize );
- end loop;
- end if;
- exception
- when Ada.IO_Exceptions.Data_Error => Terminal.Write_Line ( "One of arguments was too big. Aborting. Aborting." );
- end Creator;
- God: Creator;
- -- devil: destructor;
- begin
- null;
- end Euler003;