http://github.com/darkestkhan/project_euler
Ada | 101 lines | 65 code | 7 blank | 29 comment | 9 complexity | 65ba66094cb60652a622d7ac3fa0ecfa MD5 | raw file
``````
------------------------------------------------------------------------------
-- Author: darkestkhan                                                      --
-- Email:  darkestkhan@gmail.com                                            --
-- ( see license.txt file )                                                 --
-- Copyright Â 2011 darkestkhan                                             --
------------------------------------------------------------------------------
-- This Program is Free Software: You Can Redistribute It and/or Modify     --
-- 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;
procedure Euler003 is

Terminal: TTY.TTY;

task type Largest_Factor ( To_Factorize: Long_Integer );
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 );
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 );

To_Factorize: Long_Integer;
begin
if Argc = 0 then
Terminal.Write_Line ( "Usage: " & Ada.Command_Line.Command_Name & " [int] ... " );
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;

``````