Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Greatest program to find the largest prime factor

Name: Anonymous 2016-02-21 1:30

I was asked this question by project euler -
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

I think I came with the greatest solution in the world.
I challenge anyone to do better than this.
It's 10 lines of code and finds the solution in less than 10 milliseconds.

Seriously, you can try as much as you want, but this solution is already the best in the world.

#include <stdio.h>
int main(void)
{
int i = 2;
long n = 600851475143;
for (; n > 1; i++)
for (; n % i == 0; n /= i)
printf("%d\n", i);
return 0;
}

Name: Anonymous 2016-02-21 1:48

>>2
Don't run this. It deleted all my Toohoo pictures.

Name: Anonymous 2016-02-21 1:55

>>2
This brings us to the big question -
Does ReactOS run 2-hu games already?

>>2
This program prints numbers, it doesn't touch your files.
Toohoo pictures are sacred and are not deleted.
The answer is 6857.
There's no better program in the world to achieve this.
ENJOY!

Name: Anonymous 2016-02-21 2:00

ToeHoe

Name: Anonymous 2016-02-21 3:13

I ran it in praxis:

do
local i = 2
local n = 600851475143
while n > 1 do
while n%i == 0 do
print2(i)
n = n / i
end
i = i + 1
end
end

71
839
1471
6857

Name: Anonymous 2016-02-21 10:48

It's a simple and naive solution, running in O(GreatestPrimeFactor(n)) time, i.e. O(n) for primes.
Test it on 60129542116 instead. It's about the size of your test case.
There are better algorithms to solve this problem.

Name: Anonymous 2016-02-21 13:35

>>6
Why are you lying?

Name: Anonymous 2016-02-21 13:46

>>7
Too weak to sit up.

Name: Anonymous 2016-02-21 13:46

>>6
Tested for your number and it still gave me the answer in less than 10 milliseconds.
real 0m 0.00s
You say there's a better algorithm for this¿ Then show it!

Name: Anonymous 2016-02-21 14:40

What's >>1-san algorithm?
I tried a straight-forward solution of finding a divisor of the number and then checking if it's a prime (using the fact you only need to check the sqrt of it) from the top to bottom, so it eventually shows the largest prime factor of the number (6857) and then finishes.

It takes 0.81 s to complete. so it's at least 80 times slower than >>1-sama's solution.
Can someone explain his program?

>>5
Nice! It's been a long time I played with praxis. It was a bit bloated for me, but had some nice ideas and gave me some fun.

Name: Anonymous 2016-02-21 16:43

print 6857

Name: Anonymous 2016-02-24 15:34

>>10
the code is like 3 lines. can't you really understand it?

you suck bro.

Name: Anonymous 2016-02-24 16:03

I can understand what it does, but I can't understand how it yields in the prime factors of the input number.

Name: Anonymous 2016-02-24 17:40

>>1
Dear OP, first of all you are measuring the performance of process startup and, to a lesser extent, the write() syscall. You could've noticed it if you inserted a loop in your program and made it store solutions instead of printing, and discovered that you have to repeat it about 1000 times before you get noticeable delay.

You could also figure it from the general principles, that no way performing about 7000 divisions should take 10ms, that's 1428ns per loop iteration, you are clearly about two orders of magnitude off.

Anyway, per my own experiments your code runs in about 0.25ms/loop (full solution), not checking odd numbers predictably speeds it up twofold, modifying it as follows speeds it up fivefold on top of that.

for(long limit = sqrt(n) + 1; i < limit; i += 2)
for (;n % i == 0; n /= i, limit = sqrt(n) + 1)
solutions.insert(i);
solutions.insert(n);

Name: Anonymous 2016-02-25 3:07

>>13
as you divide n by each prime factor, the next prime factor becomes the smallest number that will divide n thus by the end you will be left with the largest prime factor.

Name: Anonymous 2016-02-25 12:07

Thanks. I understand it now.

Name: Anonymous 2016-02-25 23:46

Wheel factorization, bitch.

Name: Anonymous 2016-02-27 6:26

project euler

Go to some real programming problem solving site like acm.timus.ru for example.

Name: Anonymous 2016-02-27 11:57

>>18
Thanks for the link. Russian programmers must be awesome if they routinely solve problems at this level.

Name: Anonymous 2016-02-27 15:17

>>18
What did you do to Nikita, motherfucker!?

Name: Anonymous 2016-02-28 2:28

printf("6857\n");

Name: Anonymous 2016-02-28 13:32

_ _
>(')____, >(')____,
(` =~~/ (` =~~/
~^~^`---'~^~^~^`---'~^

Name: Anonymous 2016-03-03 15:05

>>19

Check the top authors, they solve N=NP questions like breakfast.

Anyway, it's a great site to polish your programming skill with those low tier problems.

Don't change these.
Name: Email:
Entire Thread Thread List