Largest difference between adjacent primes: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 253: | Line 253: | ||
<small>Almost all the time is spent constructing the list of primes. It is about 4 times slower under pwa/p2js so |
<small>Almost all the time is spent constructing the list of primes. It is about 4 times slower under pwa/p2js so |
||
limited to 1e8 when running in a browser to keep the time below 5s instead of over 50s.</small> |
limited to 1e8 when running in a browser to keep the time below 5s instead of over 50s.</small> |
||
=={{header|Python}}== |
|||
<lang python> |
|||
print("working...") |
|||
limit = 1000000 |
|||
res1 = 0 |
|||
res2 = 0 |
|||
maxOld = 0 |
|||
newDiff = 0 |
|||
oldDiff = 0 |
|||
def isprime(n: int) -> bool: |
|||
if n <= 3: |
|||
return n > 1 |
|||
if n % 2 == 0 or n % 3 == 0: |
|||
return False |
|||
i = 5 |
|||
while i ** 2 <= n: |
|||
if n % i == 0 or n % (i + 2) == 0: |
|||
return False |
|||
i += 6 |
|||
return True |
|||
for n in range(limit): |
|||
newDiff = n - maxOld |
|||
if isprime(n): |
|||
if (newDiff > oldDiff): |
|||
res1 = n |
|||
res2 = maxOld |
|||
oldDiff = newDiff |
|||
maxOld = n |
|||
diff = res1 - res2 |
|||
print(res1) |
|||
print(res2) |
|||
print("Largest difference is = ",end="") |
|||
print(diff) |
|||
print("done...") |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
492227 |
|||
492113 |
|||
Largest difference is = 114 |
|||
</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |