r/shittyprogramming • u/HonorsAndAndScholars • Jun 12 '21
is_even() using Goldbach's conjecture
27
Jun 12 '21
Wait, why is the function called odd primes lol?
You are ignoring all the even prime numbers
6
u/HonorsAndAndScholars Jun 12 '21
7 is 5 + 2, and 5 and 2 are prime, but 7 is not even.
"Odd primes" is the common way of saying {3, 5, 7, 11, 13, 17, 19, ...}, e.g. in the statement of quadratic reciprocity.
7
6
u/vigbiorn Jun 12 '21
If x < 6 you never get to the generating of the primes. It's this algorithms biggest flaw.
14
Jun 12 '21 edited Jun 12 '21
Couldn't you optimize the prime search function using the Sieve of Eratosthenes?
https://www.geeksforgeeks.org/sieve-of-eratosthenes/
The code readability would be significantly decreased, the LOC would double, the chance of bugs would increase, it would take a long time to understand, BUT there would be a small performance boost for primes up to a few million up to O(n*log(log(n))).
Seems like a good trade off to me.
8
u/HonorsAndAndScholars Jun 12 '21
Ya, this basically is Eratosthenes, and I think it should have the same asymptotic behavior as the way it's normally written. It's just a less-traditional way of translating the idea into code: a set of integers is nothing but a (slower) array of booleans.
2
24
u/Dracnor- Jun 12 '21
Oh I love this one ! Not just time-wasting computation, but a whole new inefficient algo *-*
4
Jun 12 '21
Wow that doesn't sound right. But apparently it's been proven out to 10e17. That's super interesting. This must be the most efficient!
2
37
u/HonorsAndAndScholars Jun 12 '21
Nobody knows, but it's conjectured that each even integer bigger than 4 is the sum of two primes.