Talk:De Polignac numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
(commentary)
 
 
(One intermediate revision by one other user not shown)
Line 4: Line 4:
It is not necessary to test add every power of 2 less than N with every prime less than N and check if the sum is N.
It is not necessary to test add every power of 2 less than N with every prime less than N and check if the sum is N.


Simply find the powers of 2 less than N, subtract N from each, and check if the remainder is a prime. If any is, it is '''not''' a de Polignac number. Short circuit and move on. --[[User:Thundergnat|Thundergnat]] ([[User talk:Thundergnat|talk]]) 22:04, 27 September 2022 (UTC)
Simply find the powers of 2 less than N, subtract each from N, and check if any remainder is a prime. If any is, it is '''not''' a de Polignac number. Short circuit and move on. --[[User:Thundergnat|Thundergnat]] ([[User talk:Thundergnat|talk]]) 22:04, 27 September 2022 (UTC)
:2nd sentence reads better if you delete "test" and replace "with" with "to". 3rd & 4th sentences would make a wonderful comment for the Raku entry 😉 --[[User:Petelomax|Petelomax]] ([[User talk:Petelomax|talk]]) 11:54, 28 September 2022 (UTC)

Latest revision as of 11:54, 28 September 2022

Efficient algorithm

There is a quite efficient algorithm to find de Polignac numbers that several entry authors seem to have overlooked.

It is not necessary to test add every power of 2 less than N with every prime less than N and check if the sum is N.

Simply find the powers of 2 less than N, subtract each from N, and check if any remainder is a prime. If any is, it is not a de Polignac number. Short circuit and move on. --Thundergnat (talk) 22:04, 27 September 2022 (UTC)

2nd sentence reads better if you delete "test" and replace "with" with "to". 3rd & 4th sentences would make a wonderful comment for the Raku entry 😉 --Petelomax (talk) 11:54, 28 September 2022 (UTC)