Goldbach's conjecture algorithm












7












$begingroup$


I did a Goldbach's Conjecture exercise and got it to work. It's pretty slow though and I was wondering how I could optimize it.



number = int(input("Enter your number >> "))
print("nCalculating...")
if number % 2 == 0: #Only even numbers
primes = primenums(number) #returns all prime numbers <= input number
addend1 = primes[0]
addend2 = primes[0]

while addend1 + addend2 != number:
if primes[-1] == addend2:
addend2 = primes[primes.index(addend1) + 1]
addend1 = primes[primes.index(addend1) + 1]
else:
addend2 = primes[primes.index(addend2) + 1]


Right now, up to 10,000 the algorithm is pretty fast, but at 100,000 it takes about 3 seconds to finish. Is that just how it is or could I make it faster?










share|improve this question









New contributor




Kevin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 5




    $begingroup$
    Have you profiled this? How is primenums implemented?
    $endgroup$
    – Graipher
    19 hours ago






  • 1




    $begingroup$
    Profiled? What do you mean?
    $endgroup$
    – Kevin
    14 hours ago






  • 3




    $begingroup$
    @EricLippert Don't worry about language/compiler/interpreter optimization before you do mathematical/complexity optimization first. Switching languages can only improve performance by a small factor (1-10). Changing the algorithm can give speedups of 1-infinity.
    $endgroup$
    – Vortico
    9 hours ago








  • 2




    $begingroup$
    @Vortico are you seriously lecturing Eric Lippert on performance gains and optimization?
    $endgroup$
    – MikeTheLiar
    8 hours ago








  • 2




    $begingroup$
    @MikeTheLiar I know he knows, but someone reading his comment may be misled. It's bad advice to recommend to a programming beginner to change the tool he's learning before even pointing out the issue with his algorithm. It's discouraging, doesn't solve the problem at hand (because maybe his goal is to simply learn Python, and remember this is a code review community), and doesn't teach how to think on one's own. It's not "The first problem" with his code as he said. It's the 26th or maybe 126th problem he should worry about while playing around with Goldbach's conjecture.
    $endgroup$
    – Vortico
    7 hours ago


















7












$begingroup$


I did a Goldbach's Conjecture exercise and got it to work. It's pretty slow though and I was wondering how I could optimize it.



number = int(input("Enter your number >> "))
print("nCalculating...")
if number % 2 == 0: #Only even numbers
primes = primenums(number) #returns all prime numbers <= input number
addend1 = primes[0]
addend2 = primes[0]

while addend1 + addend2 != number:
if primes[-1] == addend2:
addend2 = primes[primes.index(addend1) + 1]
addend1 = primes[primes.index(addend1) + 1]
else:
addend2 = primes[primes.index(addend2) + 1]


Right now, up to 10,000 the algorithm is pretty fast, but at 100,000 it takes about 3 seconds to finish. Is that just how it is or could I make it faster?










share|improve this question









New contributor




Kevin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 5




    $begingroup$
    Have you profiled this? How is primenums implemented?
    $endgroup$
    – Graipher
    19 hours ago






  • 1




    $begingroup$
    Profiled? What do you mean?
    $endgroup$
    – Kevin
    14 hours ago






  • 3




    $begingroup$
    @EricLippert Don't worry about language/compiler/interpreter optimization before you do mathematical/complexity optimization first. Switching languages can only improve performance by a small factor (1-10). Changing the algorithm can give speedups of 1-infinity.
    $endgroup$
    – Vortico
    9 hours ago








  • 2




    $begingroup$
    @Vortico are you seriously lecturing Eric Lippert on performance gains and optimization?
    $endgroup$
    – MikeTheLiar
    8 hours ago








  • 2




    $begingroup$
    @MikeTheLiar I know he knows, but someone reading his comment may be misled. It's bad advice to recommend to a programming beginner to change the tool he's learning before even pointing out the issue with his algorithm. It's discouraging, doesn't solve the problem at hand (because maybe his goal is to simply learn Python, and remember this is a code review community), and doesn't teach how to think on one's own. It's not "The first problem" with his code as he said. It's the 26th or maybe 126th problem he should worry about while playing around with Goldbach's conjecture.
    $endgroup$
    – Vortico
    7 hours ago
















7












7








7


2



$begingroup$


I did a Goldbach's Conjecture exercise and got it to work. It's pretty slow though and I was wondering how I could optimize it.



number = int(input("Enter your number >> "))
print("nCalculating...")
if number % 2 == 0: #Only even numbers
primes = primenums(number) #returns all prime numbers <= input number
addend1 = primes[0]
addend2 = primes[0]

while addend1 + addend2 != number:
if primes[-1] == addend2:
addend2 = primes[primes.index(addend1) + 1]
addend1 = primes[primes.index(addend1) + 1]
else:
addend2 = primes[primes.index(addend2) + 1]


Right now, up to 10,000 the algorithm is pretty fast, but at 100,000 it takes about 3 seconds to finish. Is that just how it is or could I make it faster?










share|improve this question









New contributor




Kevin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I did a Goldbach's Conjecture exercise and got it to work. It's pretty slow though and I was wondering how I could optimize it.



number = int(input("Enter your number >> "))
print("nCalculating...")
if number % 2 == 0: #Only even numbers
primes = primenums(number) #returns all prime numbers <= input number
addend1 = primes[0]
addend2 = primes[0]

while addend1 + addend2 != number:
if primes[-1] == addend2:
addend2 = primes[primes.index(addend1) + 1]
addend1 = primes[primes.index(addend1) + 1]
else:
addend2 = primes[primes.index(addend2) + 1]


Right now, up to 10,000 the algorithm is pretty fast, but at 100,000 it takes about 3 seconds to finish. Is that just how it is or could I make it faster?







python performance






share|improve this question









New contributor




Kevin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Kevin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 4 mins ago









Jamal

30.3k11118227




30.3k11118227






New contributor




Kevin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 19 hours ago









KevinKevin

363




363




New contributor




Kevin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Kevin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Kevin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 5




    $begingroup$
    Have you profiled this? How is primenums implemented?
    $endgroup$
    – Graipher
    19 hours ago






  • 1




    $begingroup$
    Profiled? What do you mean?
    $endgroup$
    – Kevin
    14 hours ago






  • 3




    $begingroup$
    @EricLippert Don't worry about language/compiler/interpreter optimization before you do mathematical/complexity optimization first. Switching languages can only improve performance by a small factor (1-10). Changing the algorithm can give speedups of 1-infinity.
    $endgroup$
    – Vortico
    9 hours ago








  • 2




    $begingroup$
    @Vortico are you seriously lecturing Eric Lippert on performance gains and optimization?
    $endgroup$
    – MikeTheLiar
    8 hours ago








  • 2




    $begingroup$
    @MikeTheLiar I know he knows, but someone reading his comment may be misled. It's bad advice to recommend to a programming beginner to change the tool he's learning before even pointing out the issue with his algorithm. It's discouraging, doesn't solve the problem at hand (because maybe his goal is to simply learn Python, and remember this is a code review community), and doesn't teach how to think on one's own. It's not "The first problem" with his code as he said. It's the 26th or maybe 126th problem he should worry about while playing around with Goldbach's conjecture.
    $endgroup$
    – Vortico
    7 hours ago
















  • 5




    $begingroup$
    Have you profiled this? How is primenums implemented?
    $endgroup$
    – Graipher
    19 hours ago






  • 1




    $begingroup$
    Profiled? What do you mean?
    $endgroup$
    – Kevin
    14 hours ago






  • 3




    $begingroup$
    @EricLippert Don't worry about language/compiler/interpreter optimization before you do mathematical/complexity optimization first. Switching languages can only improve performance by a small factor (1-10). Changing the algorithm can give speedups of 1-infinity.
    $endgroup$
    – Vortico
    9 hours ago








  • 2




    $begingroup$
    @Vortico are you seriously lecturing Eric Lippert on performance gains and optimization?
    $endgroup$
    – MikeTheLiar
    8 hours ago








  • 2




    $begingroup$
    @MikeTheLiar I know he knows, but someone reading his comment may be misled. It's bad advice to recommend to a programming beginner to change the tool he's learning before even pointing out the issue with his algorithm. It's discouraging, doesn't solve the problem at hand (because maybe his goal is to simply learn Python, and remember this is a code review community), and doesn't teach how to think on one's own. It's not "The first problem" with his code as he said. It's the 26th or maybe 126th problem he should worry about while playing around with Goldbach's conjecture.
    $endgroup$
    – Vortico
    7 hours ago










5




5




$begingroup$
Have you profiled this? How is primenums implemented?
$endgroup$
– Graipher
19 hours ago




$begingroup$
Have you profiled this? How is primenums implemented?
$endgroup$
– Graipher
19 hours ago




1




1




$begingroup$
Profiled? What do you mean?
$endgroup$
– Kevin
14 hours ago




$begingroup$
Profiled? What do you mean?
$endgroup$
– Kevin
14 hours ago




3




3




$begingroup$
@EricLippert Don't worry about language/compiler/interpreter optimization before you do mathematical/complexity optimization first. Switching languages can only improve performance by a small factor (1-10). Changing the algorithm can give speedups of 1-infinity.
$endgroup$
– Vortico
9 hours ago






$begingroup$
@EricLippert Don't worry about language/compiler/interpreter optimization before you do mathematical/complexity optimization first. Switching languages can only improve performance by a small factor (1-10). Changing the algorithm can give speedups of 1-infinity.
$endgroup$
– Vortico
9 hours ago






2




2




$begingroup$
@Vortico are you seriously lecturing Eric Lippert on performance gains and optimization?
$endgroup$
– MikeTheLiar
8 hours ago






$begingroup$
@Vortico are you seriously lecturing Eric Lippert on performance gains and optimization?
$endgroup$
– MikeTheLiar
8 hours ago






2




2




$begingroup$
@MikeTheLiar I know he knows, but someone reading his comment may be misled. It's bad advice to recommend to a programming beginner to change the tool he's learning before even pointing out the issue with his algorithm. It's discouraging, doesn't solve the problem at hand (because maybe his goal is to simply learn Python, and remember this is a code review community), and doesn't teach how to think on one's own. It's not "The first problem" with his code as he said. It's the 26th or maybe 126th problem he should worry about while playing around with Goldbach's conjecture.
$endgroup$
– Vortico
7 hours ago






$begingroup$
@MikeTheLiar I know he knows, but someone reading his comment may be misled. It's bad advice to recommend to a programming beginner to change the tool he's learning before even pointing out the issue with his algorithm. It's discouraging, doesn't solve the problem at hand (because maybe his goal is to simply learn Python, and remember this is a code review community), and doesn't teach how to think on one's own. It's not "The first problem" with his code as he said. It's the 26th or maybe 126th problem he should worry about while playing around with Goldbach's conjecture.
$endgroup$
– Vortico
7 hours ago












3 Answers
3






active

oldest

votes


















15












$begingroup$

One thing that makes your code slow is your repeated calls to primes.index. Each of these calls needs to linearly scan primes until it finds the value you are looking for, making this $mathcal{O}(n)$. This is especially bad in the first if case since you do this twice with exactly the same argument. Instead just keep the index around in addition to the number:



def goldbach_keep_index(number):
if number % 2 == 0: #Only even numbers
primes = primenums(number) #returns all prime numbers <= input number
i, j = 0, 0
addend1 = primes[i]
addend2 = primes[j]

while addend1 + addend2 != number:
if addend2 == primes[-1]:
i = j = i + 1
addend2 = primes[i]
addend1 = primes[i]
else:
j += 1
addend2 = primes[j]
return addend1, addend2
raise ValueError("Goldbach conjecture is only defined for even numbers")


Note that I made this a function, so you can reuse it.



Your main calling code could look like this:



if __name__ == "__main__":
number = int(input("Enter your number >> "))
p1, p2 = goldbach_keep_index(number)
print(f"{p1} + {p2} = {number}")




However, what you are really doing is taking all combinations of prime numbers, without repeating yourself. So just use itertools.combinations_with_replacement:



from itertools import combinations_with_replacement

def goldbach_combinations(number):
if number % 2 == 0:
primes = primenums(number)
for addend1, addend2 in combinations_with_replacement(primes, 2):
if addend1 + addend2 == number:
return addend1, addend2
raise Exception(f"Found a counter-example to the Goldbach conjecture: {number}")
raise ValueError("Goldbach conjecture is only defined for even numbers")


In this case primes does not even need to be a list, it can just be a generator.





If you instead make primes a set, you can easily implement the idea suggested by @Josiah in their answer by just checking if number - p is in primes:



def goldbach_set(number):
if number % 2 == 0: #Only even numbers
primes = set(primenums(number)) #returns all prime numbers <= input number
for p in primes:
k = number - p
if k in primes:
return p, k
raise Exception(f"Found a counter-example to the Goldbach conjecture: {number}")
raise ValueError("Goldbach conjecture is only defined for even numbers")




And now some timing comparisons, where goldbach is your code put into a function:



def goldbach(number):
if number % 2 == 0: #Only even numbers
primes = primenums(number) #returns all prime numbers <= input number
addend1 = primes[0]
addend2 = primes[0]

while addend1 + addend2 != number:
if primes[-1] == addend2:
addend2 = primes[primes.index(addend1) + 1]
addend1 = primes[primes.index(addend1) + 1]
else:
addend2 = primes[primes.index(addend2) + 1]
return addend1, addend2
raise ValueError("Goldbach conjecture is only defined for even numbers")


And primenums is a simple sieve:



def prime_sieve(limit):
prime = [True] * limit
prime[0] = prime[1] = False

for i, is_prime in enumerate(prime):
if is_prime:
yield i
for n in range(i * i, limit, i):
prime[n] = False

def primenums(limit):
return list(prime_sieve(limit))


enter image description here



And when pulling the generation of the primes outside of the function (and calculating them up to the largest number in the plot):



enter image description here



Of course the first three functions are vastly slower than the set because they need to go through more combinations now. However, all functions are then constant time, so the increase in time comes solely from the fact that you have to consider more primes.






share|improve this answer











$endgroup$













  • $begingroup$
    Your method would raise ValueError("Goldbach conjecture is only defined for even numbers") if the user found a counterexample to the conjecture. - Although the datatype might prevent this outcome.
    $endgroup$
    – Taemyr
    17 hours ago










  • $begingroup$
    @Taemyr: True. I considered adding a custom error for that as well, but since the conjecture has been checked to be correct up to 4 x 10^18 according to Wikipedia I didn't bother...
    $endgroup$
    – Graipher
    17 hours ago












  • $begingroup$
    @Taemyr: Added it where appropriate (the OP's code and the first function in this answer will just never halt in such a case).
    $endgroup$
    – Graipher
    16 hours ago



















8












$begingroup$

As suggested by Graipher in the comment, you're probably spending a lot of time generating a list of primes. That would be worth profiling, but I can't speculate as to how it might be improved without at least seeing the code.



There are two things that stand out to me as very clear performance sinks.



The first, and easiest to change, is index. If you call index on a list, python has to check every element in the list one at a time to see whether it's the thing you're after. If you have millions of primes, every one of those index calls is secretly a hidden loop that runs millions of times.
The good news is you can resolve this easily enough: just keep another variable which stores where in the list of primes you're up to. Then you don't have to search for your place again every time you go round the loop, because you have a bookmark to it.



Second, and slightly more subtle, is the way that this is looping. You are looking for two numbers that add up to a target. If your target is 100, you might first say "what if it's 3?" and then try all primes with 3. Then you say "what if it's 5?" and try all the primes with 5. And then you do the same with 7, and so on. This means your trying all the primes with all the primes.



Instead, if you are checking whether the first prime is 3, you know that the second prime has to be 97. So all you have to do is check whether 97 is prime. If so, it's a match, if not, you can move on to checking 5 and 95, and then 7 and 93, and so on. In this way your algorithm checks each prime against one other number instead of each prime against all the others, and should be massively faster.






share|improve this answer









$endgroup$













  • $begingroup$
    Your second suggestion is great indeed. I will implement it that way! Thanks
    $endgroup$
    – Kevin
    14 hours ago










  • $begingroup$
    You can make this even more efficient by keeping track of "near misses" to your target which eliminate other integers. Suppose you were testing 102. You start by looking for 3 + some prime. 99 isn't a prime, but the nearest prime >= 99 is 101, so if you organize the search carefully, you can remember that you just found a prime pair for 104 = 3 + 99 while looking for a pair for 102, and the next even number you need to test is not 104 but 106 (unless you already found a pair for that as well. of course).
    $endgroup$
    – alephzero
    10 hours ago












  • $begingroup$
    I would default to using a set as in @Graipher's implementation, which is very fast at discovering whether something is present, but does not tell you what the nearest miss was.
    $endgroup$
    – Josiah
    7 hours ago










  • $begingroup$
    If I had to use a regular list, and if the list was sorted, then there is indeed a clever trick available. The idea is that as the small prime being tested grows, the number it needs to be added to shrinks. That means that you can keep two bookmark variables pointing at the list of primes, one at the start and moving forwards, and one at the end and moving backwards. As long as sum is too small, you move the first bookmark along. If the sum is too big, you bring the second bookmark backwards. This avoids having to scan the whole list each time. However, it's quite fiddly to get right.
    $endgroup$
    – Josiah
    7 hours ago



















0












$begingroup$

Let's take n = 1,000,000,000 for example and see what your code does.



It calculates all primes from 1 to 1 billion. That takes a while, but it gives you an array of all primes in sorted order.



It then calculates 2 + 2, 2 + 3, 2 + 5, 2 + 7, 2 + 999,999,xxx to check if one of these numbers equals 1,000,000,000. But obviously when addend1 = 2, addend2 has to be 999,999,998 to add to one billion, so you are checking tens of millions of sums unnecessarily.



It then calculates 3 + 3, 3 + 5, 3 + 7 etc., and again, addend2 would have to be 999,999,997 million. And then again for addend1 = 5, 7, 11 etc. So you are checking a huge number of sums needlessly.



Start with addend1 = first prime, addend2 = last prime in your array. If their sum is too small, replace addend1 with the next prime to make the sum larger by the smallest possible amound. If their sum is too large, replace addend2 with the previous prime. If the sum is right, you have found a solution. And once you reached addend1 > addend2, you know there is no solution. This will be very quick, since usually you don't need to check too many values for addend1.



That makes the search a lot quicker, but doesn't help with the sieve trying to find all the primes. You usually don't need all the primes, only a very small number. In the example with n = 1,000,000,000 you probably find two primes adding up to a billion with addend1 ≤ 1000 and addend2 ≥ 999,999,000.



So here's what you do: To find all primes say in the range 999,999,000 to 1,000,000,000, you need to check if these numbers are divisible by any prime up to the square root of 1,000,000,000 which is a bit less than 32,000. So first you use a sieve to find all numbers up to 32,000. Then you create a sieve that finds all primes from 999,999,000 to 1,000,000,000. You run your search algorithm until it tries to examine primes that are not in this range. This likely doesn't happen, but if it happens, you create another sieve for the numbers 999,998,000 to 999,999,000 and so on. So instead of maybe 50 million primes, you look only for maybe 50 or 100. Again, that makes it a lot faster.






share|improve this answer









$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Kevin is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f213357%2fgoldbachs-conjecture-algorithm%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    15












    $begingroup$

    One thing that makes your code slow is your repeated calls to primes.index. Each of these calls needs to linearly scan primes until it finds the value you are looking for, making this $mathcal{O}(n)$. This is especially bad in the first if case since you do this twice with exactly the same argument. Instead just keep the index around in addition to the number:



    def goldbach_keep_index(number):
    if number % 2 == 0: #Only even numbers
    primes = primenums(number) #returns all prime numbers <= input number
    i, j = 0, 0
    addend1 = primes[i]
    addend2 = primes[j]

    while addend1 + addend2 != number:
    if addend2 == primes[-1]:
    i = j = i + 1
    addend2 = primes[i]
    addend1 = primes[i]
    else:
    j += 1
    addend2 = primes[j]
    return addend1, addend2
    raise ValueError("Goldbach conjecture is only defined for even numbers")


    Note that I made this a function, so you can reuse it.



    Your main calling code could look like this:



    if __name__ == "__main__":
    number = int(input("Enter your number >> "))
    p1, p2 = goldbach_keep_index(number)
    print(f"{p1} + {p2} = {number}")




    However, what you are really doing is taking all combinations of prime numbers, without repeating yourself. So just use itertools.combinations_with_replacement:



    from itertools import combinations_with_replacement

    def goldbach_combinations(number):
    if number % 2 == 0:
    primes = primenums(number)
    for addend1, addend2 in combinations_with_replacement(primes, 2):
    if addend1 + addend2 == number:
    return addend1, addend2
    raise Exception(f"Found a counter-example to the Goldbach conjecture: {number}")
    raise ValueError("Goldbach conjecture is only defined for even numbers")


    In this case primes does not even need to be a list, it can just be a generator.





    If you instead make primes a set, you can easily implement the idea suggested by @Josiah in their answer by just checking if number - p is in primes:



    def goldbach_set(number):
    if number % 2 == 0: #Only even numbers
    primes = set(primenums(number)) #returns all prime numbers <= input number
    for p in primes:
    k = number - p
    if k in primes:
    return p, k
    raise Exception(f"Found a counter-example to the Goldbach conjecture: {number}")
    raise ValueError("Goldbach conjecture is only defined for even numbers")




    And now some timing comparisons, where goldbach is your code put into a function:



    def goldbach(number):
    if number % 2 == 0: #Only even numbers
    primes = primenums(number) #returns all prime numbers <= input number
    addend1 = primes[0]
    addend2 = primes[0]

    while addend1 + addend2 != number:
    if primes[-1] == addend2:
    addend2 = primes[primes.index(addend1) + 1]
    addend1 = primes[primes.index(addend1) + 1]
    else:
    addend2 = primes[primes.index(addend2) + 1]
    return addend1, addend2
    raise ValueError("Goldbach conjecture is only defined for even numbers")


    And primenums is a simple sieve:



    def prime_sieve(limit):
    prime = [True] * limit
    prime[0] = prime[1] = False

    for i, is_prime in enumerate(prime):
    if is_prime:
    yield i
    for n in range(i * i, limit, i):
    prime[n] = False

    def primenums(limit):
    return list(prime_sieve(limit))


    enter image description here



    And when pulling the generation of the primes outside of the function (and calculating them up to the largest number in the plot):



    enter image description here



    Of course the first three functions are vastly slower than the set because they need to go through more combinations now. However, all functions are then constant time, so the increase in time comes solely from the fact that you have to consider more primes.






    share|improve this answer











    $endgroup$













    • $begingroup$
      Your method would raise ValueError("Goldbach conjecture is only defined for even numbers") if the user found a counterexample to the conjecture. - Although the datatype might prevent this outcome.
      $endgroup$
      – Taemyr
      17 hours ago










    • $begingroup$
      @Taemyr: True. I considered adding a custom error for that as well, but since the conjecture has been checked to be correct up to 4 x 10^18 according to Wikipedia I didn't bother...
      $endgroup$
      – Graipher
      17 hours ago












    • $begingroup$
      @Taemyr: Added it where appropriate (the OP's code and the first function in this answer will just never halt in such a case).
      $endgroup$
      – Graipher
      16 hours ago
















    15












    $begingroup$

    One thing that makes your code slow is your repeated calls to primes.index. Each of these calls needs to linearly scan primes until it finds the value you are looking for, making this $mathcal{O}(n)$. This is especially bad in the first if case since you do this twice with exactly the same argument. Instead just keep the index around in addition to the number:



    def goldbach_keep_index(number):
    if number % 2 == 0: #Only even numbers
    primes = primenums(number) #returns all prime numbers <= input number
    i, j = 0, 0
    addend1 = primes[i]
    addend2 = primes[j]

    while addend1 + addend2 != number:
    if addend2 == primes[-1]:
    i = j = i + 1
    addend2 = primes[i]
    addend1 = primes[i]
    else:
    j += 1
    addend2 = primes[j]
    return addend1, addend2
    raise ValueError("Goldbach conjecture is only defined for even numbers")


    Note that I made this a function, so you can reuse it.



    Your main calling code could look like this:



    if __name__ == "__main__":
    number = int(input("Enter your number >> "))
    p1, p2 = goldbach_keep_index(number)
    print(f"{p1} + {p2} = {number}")




    However, what you are really doing is taking all combinations of prime numbers, without repeating yourself. So just use itertools.combinations_with_replacement:



    from itertools import combinations_with_replacement

    def goldbach_combinations(number):
    if number % 2 == 0:
    primes = primenums(number)
    for addend1, addend2 in combinations_with_replacement(primes, 2):
    if addend1 + addend2 == number:
    return addend1, addend2
    raise Exception(f"Found a counter-example to the Goldbach conjecture: {number}")
    raise ValueError("Goldbach conjecture is only defined for even numbers")


    In this case primes does not even need to be a list, it can just be a generator.





    If you instead make primes a set, you can easily implement the idea suggested by @Josiah in their answer by just checking if number - p is in primes:



    def goldbach_set(number):
    if number % 2 == 0: #Only even numbers
    primes = set(primenums(number)) #returns all prime numbers <= input number
    for p in primes:
    k = number - p
    if k in primes:
    return p, k
    raise Exception(f"Found a counter-example to the Goldbach conjecture: {number}")
    raise ValueError("Goldbach conjecture is only defined for even numbers")




    And now some timing comparisons, where goldbach is your code put into a function:



    def goldbach(number):
    if number % 2 == 0: #Only even numbers
    primes = primenums(number) #returns all prime numbers <= input number
    addend1 = primes[0]
    addend2 = primes[0]

    while addend1 + addend2 != number:
    if primes[-1] == addend2:
    addend2 = primes[primes.index(addend1) + 1]
    addend1 = primes[primes.index(addend1) + 1]
    else:
    addend2 = primes[primes.index(addend2) + 1]
    return addend1, addend2
    raise ValueError("Goldbach conjecture is only defined for even numbers")


    And primenums is a simple sieve:



    def prime_sieve(limit):
    prime = [True] * limit
    prime[0] = prime[1] = False

    for i, is_prime in enumerate(prime):
    if is_prime:
    yield i
    for n in range(i * i, limit, i):
    prime[n] = False

    def primenums(limit):
    return list(prime_sieve(limit))


    enter image description here



    And when pulling the generation of the primes outside of the function (and calculating them up to the largest number in the plot):



    enter image description here



    Of course the first three functions are vastly slower than the set because they need to go through more combinations now. However, all functions are then constant time, so the increase in time comes solely from the fact that you have to consider more primes.






    share|improve this answer











    $endgroup$













    • $begingroup$
      Your method would raise ValueError("Goldbach conjecture is only defined for even numbers") if the user found a counterexample to the conjecture. - Although the datatype might prevent this outcome.
      $endgroup$
      – Taemyr
      17 hours ago










    • $begingroup$
      @Taemyr: True. I considered adding a custom error for that as well, but since the conjecture has been checked to be correct up to 4 x 10^18 according to Wikipedia I didn't bother...
      $endgroup$
      – Graipher
      17 hours ago












    • $begingroup$
      @Taemyr: Added it where appropriate (the OP's code and the first function in this answer will just never halt in such a case).
      $endgroup$
      – Graipher
      16 hours ago














    15












    15








    15





    $begingroup$

    One thing that makes your code slow is your repeated calls to primes.index. Each of these calls needs to linearly scan primes until it finds the value you are looking for, making this $mathcal{O}(n)$. This is especially bad in the first if case since you do this twice with exactly the same argument. Instead just keep the index around in addition to the number:



    def goldbach_keep_index(number):
    if number % 2 == 0: #Only even numbers
    primes = primenums(number) #returns all prime numbers <= input number
    i, j = 0, 0
    addend1 = primes[i]
    addend2 = primes[j]

    while addend1 + addend2 != number:
    if addend2 == primes[-1]:
    i = j = i + 1
    addend2 = primes[i]
    addend1 = primes[i]
    else:
    j += 1
    addend2 = primes[j]
    return addend1, addend2
    raise ValueError("Goldbach conjecture is only defined for even numbers")


    Note that I made this a function, so you can reuse it.



    Your main calling code could look like this:



    if __name__ == "__main__":
    number = int(input("Enter your number >> "))
    p1, p2 = goldbach_keep_index(number)
    print(f"{p1} + {p2} = {number}")




    However, what you are really doing is taking all combinations of prime numbers, without repeating yourself. So just use itertools.combinations_with_replacement:



    from itertools import combinations_with_replacement

    def goldbach_combinations(number):
    if number % 2 == 0:
    primes = primenums(number)
    for addend1, addend2 in combinations_with_replacement(primes, 2):
    if addend1 + addend2 == number:
    return addend1, addend2
    raise Exception(f"Found a counter-example to the Goldbach conjecture: {number}")
    raise ValueError("Goldbach conjecture is only defined for even numbers")


    In this case primes does not even need to be a list, it can just be a generator.





    If you instead make primes a set, you can easily implement the idea suggested by @Josiah in their answer by just checking if number - p is in primes:



    def goldbach_set(number):
    if number % 2 == 0: #Only even numbers
    primes = set(primenums(number)) #returns all prime numbers <= input number
    for p in primes:
    k = number - p
    if k in primes:
    return p, k
    raise Exception(f"Found a counter-example to the Goldbach conjecture: {number}")
    raise ValueError("Goldbach conjecture is only defined for even numbers")




    And now some timing comparisons, where goldbach is your code put into a function:



    def goldbach(number):
    if number % 2 == 0: #Only even numbers
    primes = primenums(number) #returns all prime numbers <= input number
    addend1 = primes[0]
    addend2 = primes[0]

    while addend1 + addend2 != number:
    if primes[-1] == addend2:
    addend2 = primes[primes.index(addend1) + 1]
    addend1 = primes[primes.index(addend1) + 1]
    else:
    addend2 = primes[primes.index(addend2) + 1]
    return addend1, addend2
    raise ValueError("Goldbach conjecture is only defined for even numbers")


    And primenums is a simple sieve:



    def prime_sieve(limit):
    prime = [True] * limit
    prime[0] = prime[1] = False

    for i, is_prime in enumerate(prime):
    if is_prime:
    yield i
    for n in range(i * i, limit, i):
    prime[n] = False

    def primenums(limit):
    return list(prime_sieve(limit))


    enter image description here



    And when pulling the generation of the primes outside of the function (and calculating them up to the largest number in the plot):



    enter image description here



    Of course the first three functions are vastly slower than the set because they need to go through more combinations now. However, all functions are then constant time, so the increase in time comes solely from the fact that you have to consider more primes.






    share|improve this answer











    $endgroup$



    One thing that makes your code slow is your repeated calls to primes.index. Each of these calls needs to linearly scan primes until it finds the value you are looking for, making this $mathcal{O}(n)$. This is especially bad in the first if case since you do this twice with exactly the same argument. Instead just keep the index around in addition to the number:



    def goldbach_keep_index(number):
    if number % 2 == 0: #Only even numbers
    primes = primenums(number) #returns all prime numbers <= input number
    i, j = 0, 0
    addend1 = primes[i]
    addend2 = primes[j]

    while addend1 + addend2 != number:
    if addend2 == primes[-1]:
    i = j = i + 1
    addend2 = primes[i]
    addend1 = primes[i]
    else:
    j += 1
    addend2 = primes[j]
    return addend1, addend2
    raise ValueError("Goldbach conjecture is only defined for even numbers")


    Note that I made this a function, so you can reuse it.



    Your main calling code could look like this:



    if __name__ == "__main__":
    number = int(input("Enter your number >> "))
    p1, p2 = goldbach_keep_index(number)
    print(f"{p1} + {p2} = {number}")




    However, what you are really doing is taking all combinations of prime numbers, without repeating yourself. So just use itertools.combinations_with_replacement:



    from itertools import combinations_with_replacement

    def goldbach_combinations(number):
    if number % 2 == 0:
    primes = primenums(number)
    for addend1, addend2 in combinations_with_replacement(primes, 2):
    if addend1 + addend2 == number:
    return addend1, addend2
    raise Exception(f"Found a counter-example to the Goldbach conjecture: {number}")
    raise ValueError("Goldbach conjecture is only defined for even numbers")


    In this case primes does not even need to be a list, it can just be a generator.





    If you instead make primes a set, you can easily implement the idea suggested by @Josiah in their answer by just checking if number - p is in primes:



    def goldbach_set(number):
    if number % 2 == 0: #Only even numbers
    primes = set(primenums(number)) #returns all prime numbers <= input number
    for p in primes:
    k = number - p
    if k in primes:
    return p, k
    raise Exception(f"Found a counter-example to the Goldbach conjecture: {number}")
    raise ValueError("Goldbach conjecture is only defined for even numbers")




    And now some timing comparisons, where goldbach is your code put into a function:



    def goldbach(number):
    if number % 2 == 0: #Only even numbers
    primes = primenums(number) #returns all prime numbers <= input number
    addend1 = primes[0]
    addend2 = primes[0]

    while addend1 + addend2 != number:
    if primes[-1] == addend2:
    addend2 = primes[primes.index(addend1) + 1]
    addend1 = primes[primes.index(addend1) + 1]
    else:
    addend2 = primes[primes.index(addend2) + 1]
    return addend1, addend2
    raise ValueError("Goldbach conjecture is only defined for even numbers")


    And primenums is a simple sieve:



    def prime_sieve(limit):
    prime = [True] * limit
    prime[0] = prime[1] = False

    for i, is_prime in enumerate(prime):
    if is_prime:
    yield i
    for n in range(i * i, limit, i):
    prime[n] = False

    def primenums(limit):
    return list(prime_sieve(limit))


    enter image description here



    And when pulling the generation of the primes outside of the function (and calculating them up to the largest number in the plot):



    enter image description here



    Of course the first three functions are vastly slower than the set because they need to go through more combinations now. However, all functions are then constant time, so the increase in time comes solely from the fact that you have to consider more primes.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 16 hours ago

























    answered 19 hours ago









    GraipherGraipher

    24.6k53587




    24.6k53587












    • $begingroup$
      Your method would raise ValueError("Goldbach conjecture is only defined for even numbers") if the user found a counterexample to the conjecture. - Although the datatype might prevent this outcome.
      $endgroup$
      – Taemyr
      17 hours ago










    • $begingroup$
      @Taemyr: True. I considered adding a custom error for that as well, but since the conjecture has been checked to be correct up to 4 x 10^18 according to Wikipedia I didn't bother...
      $endgroup$
      – Graipher
      17 hours ago












    • $begingroup$
      @Taemyr: Added it where appropriate (the OP's code and the first function in this answer will just never halt in such a case).
      $endgroup$
      – Graipher
      16 hours ago


















    • $begingroup$
      Your method would raise ValueError("Goldbach conjecture is only defined for even numbers") if the user found a counterexample to the conjecture. - Although the datatype might prevent this outcome.
      $endgroup$
      – Taemyr
      17 hours ago










    • $begingroup$
      @Taemyr: True. I considered adding a custom error for that as well, but since the conjecture has been checked to be correct up to 4 x 10^18 according to Wikipedia I didn't bother...
      $endgroup$
      – Graipher
      17 hours ago












    • $begingroup$
      @Taemyr: Added it where appropriate (the OP's code and the first function in this answer will just never halt in such a case).
      $endgroup$
      – Graipher
      16 hours ago
















    $begingroup$
    Your method would raise ValueError("Goldbach conjecture is only defined for even numbers") if the user found a counterexample to the conjecture. - Although the datatype might prevent this outcome.
    $endgroup$
    – Taemyr
    17 hours ago




    $begingroup$
    Your method would raise ValueError("Goldbach conjecture is only defined for even numbers") if the user found a counterexample to the conjecture. - Although the datatype might prevent this outcome.
    $endgroup$
    – Taemyr
    17 hours ago












    $begingroup$
    @Taemyr: True. I considered adding a custom error for that as well, but since the conjecture has been checked to be correct up to 4 x 10^18 according to Wikipedia I didn't bother...
    $endgroup$
    – Graipher
    17 hours ago






    $begingroup$
    @Taemyr: True. I considered adding a custom error for that as well, but since the conjecture has been checked to be correct up to 4 x 10^18 according to Wikipedia I didn't bother...
    $endgroup$
    – Graipher
    17 hours ago














    $begingroup$
    @Taemyr: Added it where appropriate (the OP's code and the first function in this answer will just never halt in such a case).
    $endgroup$
    – Graipher
    16 hours ago




    $begingroup$
    @Taemyr: Added it where appropriate (the OP's code and the first function in this answer will just never halt in such a case).
    $endgroup$
    – Graipher
    16 hours ago













    8












    $begingroup$

    As suggested by Graipher in the comment, you're probably spending a lot of time generating a list of primes. That would be worth profiling, but I can't speculate as to how it might be improved without at least seeing the code.



    There are two things that stand out to me as very clear performance sinks.



    The first, and easiest to change, is index. If you call index on a list, python has to check every element in the list one at a time to see whether it's the thing you're after. If you have millions of primes, every one of those index calls is secretly a hidden loop that runs millions of times.
    The good news is you can resolve this easily enough: just keep another variable which stores where in the list of primes you're up to. Then you don't have to search for your place again every time you go round the loop, because you have a bookmark to it.



    Second, and slightly more subtle, is the way that this is looping. You are looking for two numbers that add up to a target. If your target is 100, you might first say "what if it's 3?" and then try all primes with 3. Then you say "what if it's 5?" and try all the primes with 5. And then you do the same with 7, and so on. This means your trying all the primes with all the primes.



    Instead, if you are checking whether the first prime is 3, you know that the second prime has to be 97. So all you have to do is check whether 97 is prime. If so, it's a match, if not, you can move on to checking 5 and 95, and then 7 and 93, and so on. In this way your algorithm checks each prime against one other number instead of each prime against all the others, and should be massively faster.






    share|improve this answer









    $endgroup$













    • $begingroup$
      Your second suggestion is great indeed. I will implement it that way! Thanks
      $endgroup$
      – Kevin
      14 hours ago










    • $begingroup$
      You can make this even more efficient by keeping track of "near misses" to your target which eliminate other integers. Suppose you were testing 102. You start by looking for 3 + some prime. 99 isn't a prime, but the nearest prime >= 99 is 101, so if you organize the search carefully, you can remember that you just found a prime pair for 104 = 3 + 99 while looking for a pair for 102, and the next even number you need to test is not 104 but 106 (unless you already found a pair for that as well. of course).
      $endgroup$
      – alephzero
      10 hours ago












    • $begingroup$
      I would default to using a set as in @Graipher's implementation, which is very fast at discovering whether something is present, but does not tell you what the nearest miss was.
      $endgroup$
      – Josiah
      7 hours ago










    • $begingroup$
      If I had to use a regular list, and if the list was sorted, then there is indeed a clever trick available. The idea is that as the small prime being tested grows, the number it needs to be added to shrinks. That means that you can keep two bookmark variables pointing at the list of primes, one at the start and moving forwards, and one at the end and moving backwards. As long as sum is too small, you move the first bookmark along. If the sum is too big, you bring the second bookmark backwards. This avoids having to scan the whole list each time. However, it's quite fiddly to get right.
      $endgroup$
      – Josiah
      7 hours ago
















    8












    $begingroup$

    As suggested by Graipher in the comment, you're probably spending a lot of time generating a list of primes. That would be worth profiling, but I can't speculate as to how it might be improved without at least seeing the code.



    There are two things that stand out to me as very clear performance sinks.



    The first, and easiest to change, is index. If you call index on a list, python has to check every element in the list one at a time to see whether it's the thing you're after. If you have millions of primes, every one of those index calls is secretly a hidden loop that runs millions of times.
    The good news is you can resolve this easily enough: just keep another variable which stores where in the list of primes you're up to. Then you don't have to search for your place again every time you go round the loop, because you have a bookmark to it.



    Second, and slightly more subtle, is the way that this is looping. You are looking for two numbers that add up to a target. If your target is 100, you might first say "what if it's 3?" and then try all primes with 3. Then you say "what if it's 5?" and try all the primes with 5. And then you do the same with 7, and so on. This means your trying all the primes with all the primes.



    Instead, if you are checking whether the first prime is 3, you know that the second prime has to be 97. So all you have to do is check whether 97 is prime. If so, it's a match, if not, you can move on to checking 5 and 95, and then 7 and 93, and so on. In this way your algorithm checks each prime against one other number instead of each prime against all the others, and should be massively faster.






    share|improve this answer









    $endgroup$













    • $begingroup$
      Your second suggestion is great indeed. I will implement it that way! Thanks
      $endgroup$
      – Kevin
      14 hours ago










    • $begingroup$
      You can make this even more efficient by keeping track of "near misses" to your target which eliminate other integers. Suppose you were testing 102. You start by looking for 3 + some prime. 99 isn't a prime, but the nearest prime >= 99 is 101, so if you organize the search carefully, you can remember that you just found a prime pair for 104 = 3 + 99 while looking for a pair for 102, and the next even number you need to test is not 104 but 106 (unless you already found a pair for that as well. of course).
      $endgroup$
      – alephzero
      10 hours ago












    • $begingroup$
      I would default to using a set as in @Graipher's implementation, which is very fast at discovering whether something is present, but does not tell you what the nearest miss was.
      $endgroup$
      – Josiah
      7 hours ago










    • $begingroup$
      If I had to use a regular list, and if the list was sorted, then there is indeed a clever trick available. The idea is that as the small prime being tested grows, the number it needs to be added to shrinks. That means that you can keep two bookmark variables pointing at the list of primes, one at the start and moving forwards, and one at the end and moving backwards. As long as sum is too small, you move the first bookmark along. If the sum is too big, you bring the second bookmark backwards. This avoids having to scan the whole list each time. However, it's quite fiddly to get right.
      $endgroup$
      – Josiah
      7 hours ago














    8












    8








    8





    $begingroup$

    As suggested by Graipher in the comment, you're probably spending a lot of time generating a list of primes. That would be worth profiling, but I can't speculate as to how it might be improved without at least seeing the code.



    There are two things that stand out to me as very clear performance sinks.



    The first, and easiest to change, is index. If you call index on a list, python has to check every element in the list one at a time to see whether it's the thing you're after. If you have millions of primes, every one of those index calls is secretly a hidden loop that runs millions of times.
    The good news is you can resolve this easily enough: just keep another variable which stores where in the list of primes you're up to. Then you don't have to search for your place again every time you go round the loop, because you have a bookmark to it.



    Second, and slightly more subtle, is the way that this is looping. You are looking for two numbers that add up to a target. If your target is 100, you might first say "what if it's 3?" and then try all primes with 3. Then you say "what if it's 5?" and try all the primes with 5. And then you do the same with 7, and so on. This means your trying all the primes with all the primes.



    Instead, if you are checking whether the first prime is 3, you know that the second prime has to be 97. So all you have to do is check whether 97 is prime. If so, it's a match, if not, you can move on to checking 5 and 95, and then 7 and 93, and so on. In this way your algorithm checks each prime against one other number instead of each prime against all the others, and should be massively faster.






    share|improve this answer









    $endgroup$



    As suggested by Graipher in the comment, you're probably spending a lot of time generating a list of primes. That would be worth profiling, but I can't speculate as to how it might be improved without at least seeing the code.



    There are two things that stand out to me as very clear performance sinks.



    The first, and easiest to change, is index. If you call index on a list, python has to check every element in the list one at a time to see whether it's the thing you're after. If you have millions of primes, every one of those index calls is secretly a hidden loop that runs millions of times.
    The good news is you can resolve this easily enough: just keep another variable which stores where in the list of primes you're up to. Then you don't have to search for your place again every time you go round the loop, because you have a bookmark to it.



    Second, and slightly more subtle, is the way that this is looping. You are looking for two numbers that add up to a target. If your target is 100, you might first say "what if it's 3?" and then try all primes with 3. Then you say "what if it's 5?" and try all the primes with 5. And then you do the same with 7, and so on. This means your trying all the primes with all the primes.



    Instead, if you are checking whether the first prime is 3, you know that the second prime has to be 97. So all you have to do is check whether 97 is prime. If so, it's a match, if not, you can move on to checking 5 and 95, and then 7 and 93, and so on. In this way your algorithm checks each prime against one other number instead of each prime against all the others, and should be massively faster.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 19 hours ago









    JosiahJosiah

    3,522427




    3,522427












    • $begingroup$
      Your second suggestion is great indeed. I will implement it that way! Thanks
      $endgroup$
      – Kevin
      14 hours ago










    • $begingroup$
      You can make this even more efficient by keeping track of "near misses" to your target which eliminate other integers. Suppose you were testing 102. You start by looking for 3 + some prime. 99 isn't a prime, but the nearest prime >= 99 is 101, so if you organize the search carefully, you can remember that you just found a prime pair for 104 = 3 + 99 while looking for a pair for 102, and the next even number you need to test is not 104 but 106 (unless you already found a pair for that as well. of course).
      $endgroup$
      – alephzero
      10 hours ago












    • $begingroup$
      I would default to using a set as in @Graipher's implementation, which is very fast at discovering whether something is present, but does not tell you what the nearest miss was.
      $endgroup$
      – Josiah
      7 hours ago










    • $begingroup$
      If I had to use a regular list, and if the list was sorted, then there is indeed a clever trick available. The idea is that as the small prime being tested grows, the number it needs to be added to shrinks. That means that you can keep two bookmark variables pointing at the list of primes, one at the start and moving forwards, and one at the end and moving backwards. As long as sum is too small, you move the first bookmark along. If the sum is too big, you bring the second bookmark backwards. This avoids having to scan the whole list each time. However, it's quite fiddly to get right.
      $endgroup$
      – Josiah
      7 hours ago


















    • $begingroup$
      Your second suggestion is great indeed. I will implement it that way! Thanks
      $endgroup$
      – Kevin
      14 hours ago










    • $begingroup$
      You can make this even more efficient by keeping track of "near misses" to your target which eliminate other integers. Suppose you were testing 102. You start by looking for 3 + some prime. 99 isn't a prime, but the nearest prime >= 99 is 101, so if you organize the search carefully, you can remember that you just found a prime pair for 104 = 3 + 99 while looking for a pair for 102, and the next even number you need to test is not 104 but 106 (unless you already found a pair for that as well. of course).
      $endgroup$
      – alephzero
      10 hours ago












    • $begingroup$
      I would default to using a set as in @Graipher's implementation, which is very fast at discovering whether something is present, but does not tell you what the nearest miss was.
      $endgroup$
      – Josiah
      7 hours ago










    • $begingroup$
      If I had to use a regular list, and if the list was sorted, then there is indeed a clever trick available. The idea is that as the small prime being tested grows, the number it needs to be added to shrinks. That means that you can keep two bookmark variables pointing at the list of primes, one at the start and moving forwards, and one at the end and moving backwards. As long as sum is too small, you move the first bookmark along. If the sum is too big, you bring the second bookmark backwards. This avoids having to scan the whole list each time. However, it's quite fiddly to get right.
      $endgroup$
      – Josiah
      7 hours ago
















    $begingroup$
    Your second suggestion is great indeed. I will implement it that way! Thanks
    $endgroup$
    – Kevin
    14 hours ago




    $begingroup$
    Your second suggestion is great indeed. I will implement it that way! Thanks
    $endgroup$
    – Kevin
    14 hours ago












    $begingroup$
    You can make this even more efficient by keeping track of "near misses" to your target which eliminate other integers. Suppose you were testing 102. You start by looking for 3 + some prime. 99 isn't a prime, but the nearest prime >= 99 is 101, so if you organize the search carefully, you can remember that you just found a prime pair for 104 = 3 + 99 while looking for a pair for 102, and the next even number you need to test is not 104 but 106 (unless you already found a pair for that as well. of course).
    $endgroup$
    – alephzero
    10 hours ago






    $begingroup$
    You can make this even more efficient by keeping track of "near misses" to your target which eliminate other integers. Suppose you were testing 102. You start by looking for 3 + some prime. 99 isn't a prime, but the nearest prime >= 99 is 101, so if you organize the search carefully, you can remember that you just found a prime pair for 104 = 3 + 99 while looking for a pair for 102, and the next even number you need to test is not 104 but 106 (unless you already found a pair for that as well. of course).
    $endgroup$
    – alephzero
    10 hours ago














    $begingroup$
    I would default to using a set as in @Graipher's implementation, which is very fast at discovering whether something is present, but does not tell you what the nearest miss was.
    $endgroup$
    – Josiah
    7 hours ago




    $begingroup$
    I would default to using a set as in @Graipher's implementation, which is very fast at discovering whether something is present, but does not tell you what the nearest miss was.
    $endgroup$
    – Josiah
    7 hours ago












    $begingroup$
    If I had to use a regular list, and if the list was sorted, then there is indeed a clever trick available. The idea is that as the small prime being tested grows, the number it needs to be added to shrinks. That means that you can keep two bookmark variables pointing at the list of primes, one at the start and moving forwards, and one at the end and moving backwards. As long as sum is too small, you move the first bookmark along. If the sum is too big, you bring the second bookmark backwards. This avoids having to scan the whole list each time. However, it's quite fiddly to get right.
    $endgroup$
    – Josiah
    7 hours ago




    $begingroup$
    If I had to use a regular list, and if the list was sorted, then there is indeed a clever trick available. The idea is that as the small prime being tested grows, the number it needs to be added to shrinks. That means that you can keep two bookmark variables pointing at the list of primes, one at the start and moving forwards, and one at the end and moving backwards. As long as sum is too small, you move the first bookmark along. If the sum is too big, you bring the second bookmark backwards. This avoids having to scan the whole list each time. However, it's quite fiddly to get right.
    $endgroup$
    – Josiah
    7 hours ago











    0












    $begingroup$

    Let's take n = 1,000,000,000 for example and see what your code does.



    It calculates all primes from 1 to 1 billion. That takes a while, but it gives you an array of all primes in sorted order.



    It then calculates 2 + 2, 2 + 3, 2 + 5, 2 + 7, 2 + 999,999,xxx to check if one of these numbers equals 1,000,000,000. But obviously when addend1 = 2, addend2 has to be 999,999,998 to add to one billion, so you are checking tens of millions of sums unnecessarily.



    It then calculates 3 + 3, 3 + 5, 3 + 7 etc., and again, addend2 would have to be 999,999,997 million. And then again for addend1 = 5, 7, 11 etc. So you are checking a huge number of sums needlessly.



    Start with addend1 = first prime, addend2 = last prime in your array. If their sum is too small, replace addend1 with the next prime to make the sum larger by the smallest possible amound. If their sum is too large, replace addend2 with the previous prime. If the sum is right, you have found a solution. And once you reached addend1 > addend2, you know there is no solution. This will be very quick, since usually you don't need to check too many values for addend1.



    That makes the search a lot quicker, but doesn't help with the sieve trying to find all the primes. You usually don't need all the primes, only a very small number. In the example with n = 1,000,000,000 you probably find two primes adding up to a billion with addend1 ≤ 1000 and addend2 ≥ 999,999,000.



    So here's what you do: To find all primes say in the range 999,999,000 to 1,000,000,000, you need to check if these numbers are divisible by any prime up to the square root of 1,000,000,000 which is a bit less than 32,000. So first you use a sieve to find all numbers up to 32,000. Then you create a sieve that finds all primes from 999,999,000 to 1,000,000,000. You run your search algorithm until it tries to examine primes that are not in this range. This likely doesn't happen, but if it happens, you create another sieve for the numbers 999,998,000 to 999,999,000 and so on. So instead of maybe 50 million primes, you look only for maybe 50 or 100. Again, that makes it a lot faster.






    share|improve this answer









    $endgroup$


















      0












      $begingroup$

      Let's take n = 1,000,000,000 for example and see what your code does.



      It calculates all primes from 1 to 1 billion. That takes a while, but it gives you an array of all primes in sorted order.



      It then calculates 2 + 2, 2 + 3, 2 + 5, 2 + 7, 2 + 999,999,xxx to check if one of these numbers equals 1,000,000,000. But obviously when addend1 = 2, addend2 has to be 999,999,998 to add to one billion, so you are checking tens of millions of sums unnecessarily.



      It then calculates 3 + 3, 3 + 5, 3 + 7 etc., and again, addend2 would have to be 999,999,997 million. And then again for addend1 = 5, 7, 11 etc. So you are checking a huge number of sums needlessly.



      Start with addend1 = first prime, addend2 = last prime in your array. If their sum is too small, replace addend1 with the next prime to make the sum larger by the smallest possible amound. If their sum is too large, replace addend2 with the previous prime. If the sum is right, you have found a solution. And once you reached addend1 > addend2, you know there is no solution. This will be very quick, since usually you don't need to check too many values for addend1.



      That makes the search a lot quicker, but doesn't help with the sieve trying to find all the primes. You usually don't need all the primes, only a very small number. In the example with n = 1,000,000,000 you probably find two primes adding up to a billion with addend1 ≤ 1000 and addend2 ≥ 999,999,000.



      So here's what you do: To find all primes say in the range 999,999,000 to 1,000,000,000, you need to check if these numbers are divisible by any prime up to the square root of 1,000,000,000 which is a bit less than 32,000. So first you use a sieve to find all numbers up to 32,000. Then you create a sieve that finds all primes from 999,999,000 to 1,000,000,000. You run your search algorithm until it tries to examine primes that are not in this range. This likely doesn't happen, but if it happens, you create another sieve for the numbers 999,998,000 to 999,999,000 and so on. So instead of maybe 50 million primes, you look only for maybe 50 or 100. Again, that makes it a lot faster.






      share|improve this answer









      $endgroup$
















        0












        0








        0





        $begingroup$

        Let's take n = 1,000,000,000 for example and see what your code does.



        It calculates all primes from 1 to 1 billion. That takes a while, but it gives you an array of all primes in sorted order.



        It then calculates 2 + 2, 2 + 3, 2 + 5, 2 + 7, 2 + 999,999,xxx to check if one of these numbers equals 1,000,000,000. But obviously when addend1 = 2, addend2 has to be 999,999,998 to add to one billion, so you are checking tens of millions of sums unnecessarily.



        It then calculates 3 + 3, 3 + 5, 3 + 7 etc., and again, addend2 would have to be 999,999,997 million. And then again for addend1 = 5, 7, 11 etc. So you are checking a huge number of sums needlessly.



        Start with addend1 = first prime, addend2 = last prime in your array. If their sum is too small, replace addend1 with the next prime to make the sum larger by the smallest possible amound. If their sum is too large, replace addend2 with the previous prime. If the sum is right, you have found a solution. And once you reached addend1 > addend2, you know there is no solution. This will be very quick, since usually you don't need to check too many values for addend1.



        That makes the search a lot quicker, but doesn't help with the sieve trying to find all the primes. You usually don't need all the primes, only a very small number. In the example with n = 1,000,000,000 you probably find two primes adding up to a billion with addend1 ≤ 1000 and addend2 ≥ 999,999,000.



        So here's what you do: To find all primes say in the range 999,999,000 to 1,000,000,000, you need to check if these numbers are divisible by any prime up to the square root of 1,000,000,000 which is a bit less than 32,000. So first you use a sieve to find all numbers up to 32,000. Then you create a sieve that finds all primes from 999,999,000 to 1,000,000,000. You run your search algorithm until it tries to examine primes that are not in this range. This likely doesn't happen, but if it happens, you create another sieve for the numbers 999,998,000 to 999,999,000 and so on. So instead of maybe 50 million primes, you look only for maybe 50 or 100. Again, that makes it a lot faster.






        share|improve this answer









        $endgroup$



        Let's take n = 1,000,000,000 for example and see what your code does.



        It calculates all primes from 1 to 1 billion. That takes a while, but it gives you an array of all primes in sorted order.



        It then calculates 2 + 2, 2 + 3, 2 + 5, 2 + 7, 2 + 999,999,xxx to check if one of these numbers equals 1,000,000,000. But obviously when addend1 = 2, addend2 has to be 999,999,998 to add to one billion, so you are checking tens of millions of sums unnecessarily.



        It then calculates 3 + 3, 3 + 5, 3 + 7 etc., and again, addend2 would have to be 999,999,997 million. And then again for addend1 = 5, 7, 11 etc. So you are checking a huge number of sums needlessly.



        Start with addend1 = first prime, addend2 = last prime in your array. If their sum is too small, replace addend1 with the next prime to make the sum larger by the smallest possible amound. If their sum is too large, replace addend2 with the previous prime. If the sum is right, you have found a solution. And once you reached addend1 > addend2, you know there is no solution. This will be very quick, since usually you don't need to check too many values for addend1.



        That makes the search a lot quicker, but doesn't help with the sieve trying to find all the primes. You usually don't need all the primes, only a very small number. In the example with n = 1,000,000,000 you probably find two primes adding up to a billion with addend1 ≤ 1000 and addend2 ≥ 999,999,000.



        So here's what you do: To find all primes say in the range 999,999,000 to 1,000,000,000, you need to check if these numbers are divisible by any prime up to the square root of 1,000,000,000 which is a bit less than 32,000. So first you use a sieve to find all numbers up to 32,000. Then you create a sieve that finds all primes from 999,999,000 to 1,000,000,000. You run your search algorithm until it tries to examine primes that are not in this range. This likely doesn't happen, but if it happens, you create another sieve for the numbers 999,998,000 to 999,999,000 and so on. So instead of maybe 50 million primes, you look only for maybe 50 or 100. Again, that makes it a lot faster.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 4 hours ago









        gnasher729gnasher729

        1,857611




        1,857611






















            Kevin is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Kevin is a new contributor. Be nice, and check out our Code of Conduct.













            Kevin is a new contributor. Be nice, and check out our Code of Conduct.












            Kevin is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f213357%2fgoldbachs-conjecture-algorithm%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python