r/dailyprogrammer • u/rya11111 3 1 • Apr 08 '12
[4/8/2012] Challenge #37 [difficult]
Your task is to implement Cornacchia’s algorithm and use it to demonstrate that all primes of the form 4k+1 and less than 1000 can be written as the sum of two squares.
source: programmingpraxis.com
11
Upvotes
1
u/robotfarts Apr 08 '12 edited Apr 08 '12
1
u/oskar_s Apr 08 '12 edited Apr 08 '12
Not the shortest program ever written, but I don't much care for unreadable one-liners anyway :). Asserts and clarity, that's my thing!
I was planning to implement some sort of advanced algorithm for finding the quadratic residue, but I though "Nahh, screw it, it's just numbers under 1000, brute force it!". As a consequence, it takes longer than I'd like if you raise the limit, for 105 it takes around 12 seconds (that's without the printing), and much longer for higher magnitudes. I'm slightly bummed about that, but whatever.
Fun problem! I hadn't heard of this algorithm before, it's very cool!
In Python:
1
u/lawlrng_prog Apr 08 '12
Chall_math is a module I wrote for various functions that I use when dealing with project euler.
My program takes a couple of assumptions into consideration. Such as that the initial r-naught will be found, and it doesn't actually do anything if the found y is not an integer. That said, all returned x's and y's worked in eval, so who knows! =)
In my own testing it worked for a prime list up to 100000 s'long as they were of the form 4k + 1.
1
u/sb3700 Apr 09 '12
1
u/Cosmologicon 2 3 Apr 09 '12
python
1
u/Ozera 0 0 Apr 10 '12
Not an answer, a question.
Why does everyone here solve problems in Python ._.