On my Mysql blog I've come up against the poser from the Euler Project : "What is the largest prime factor of the number 600851475143 ".

I think I've cracked this in Mysql, but I'm not 100% confident (well it works in this case, but will it always work?)

Anyway, here it is (I use the table Orders only as it contains plenty of rows):

*** set variables ***

set @var1=2;

set @varreset=2;

set @bignuma= 600851475143;

*** sql code ***

select a1factor from

(select

case when mod(@bignuma,@var1) = 0 then @var1:=@varreset else @var1:=@var1+1 end as a1factor,

case when mod(@bignuma,@var1) = 0 then @bignuma:=@bignuma/@var1 end as new_factor

from orders where @var1<@bignuma ) tmp

where new_factor is not null

order by a1factor desc;

## No comments:

## Post a Comment