Created
November 28, 2020 16:40
-
-
Save ov7a/04a4780b4eff276597d637c42c48ec67 to your computer and use it in GitHub Desktop.
Simple check of Buenos Aires Constant (aka prime generating constant)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import sys | |
BAC = 2.920050977316134 | |
def primes_BAC(): | |
item = BAC | |
while True: | |
next_prime = int(math.floor(item)) | |
yield next_prime | |
item = next_prime * (item - next_prime + 1) | |
def primes(): | |
yield 2 | |
current = 3 | |
primes = [2] | |
while True: | |
i = 0 | |
prime = True | |
while primes[i] * primes[i] <= current: | |
if current % primes[i] == 0: | |
prime = False | |
break | |
i += 1 | |
if prime: | |
primes.append(current) | |
yield current | |
current += 1 | |
for idx, (prime_bac, prime_simple) in enumerate(zip(primes_BAC(), primes())): | |
if prime_bac != prime_simple: | |
print(f"Failed at {idx}: generated {prime_bac}, but correct is {prime_simple}") | |
break | |
if idx % 1000 == 999: | |
print(f"Everything good up to {idx}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import sys | |
from decimal import * | |
decimal_places = int(sys.argv[1]) | |
getcontext().prec = decimal_places | |
BAC = Decimal('2.920050977316134712092562917112019468002727899321426719772682533107733772127766124190178112317583742298338859553101376655790789350859005320458879066450802184292252411229314474766432726781359401611930934817871151893525264603575379521810813046420167707977389570250111803809282686178658154888032203367829269125018738690914047486202261291216227368389829929455587891569755355387051873925153818160795717143424044663393262377616866023439422418496621696030394939023411977293803544096857594931022126398510871655055416847798060973123630164611574876105859201981461135440151697900932470462567084428160424925578714793121712974479882318073408039208192925733361718397515791447897048836410034817769998489023206039246045593238665161843153486393205335710706553090212523695784940062438701975579958564876909551757478171387837461948559472493761934243132609402333680301736898869114665897851582285975980667789889845956308635891790746893363179701184088676786462101030418130966192439808238720725713956534268643753471734394623') | |
def primes_BAC(): | |
item = BAC | |
while True: | |
next_prime = math.floor(item) | |
yield int(next_prime) | |
item = next_prime * (item - next_prime + 1) | |
def primes(): | |
yield 2 | |
current = 3 | |
primes = [2] | |
while True: | |
i = 0 | |
prime = True | |
while primes[i] * primes[i] <= current: | |
if current % primes[i] == 0: | |
prime = False | |
break | |
i += 1 | |
if prime: | |
primes.append(current) | |
yield current | |
current += 1 | |
for idx, (prime_bac, prime_simple) in enumerate(zip(primes_BAC(), primes())): | |
if prime_bac != prime_simple: | |
print(f"{decimal_places} decimal places failed at {idx}: generated {prime_bac}, but correct is {prime_simple}") | |
break | |
if idx % 1000 == 999: | |
print(f"Everything good up to {idx}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import sys | |
from decimal import * | |
n = int(sys.argv[1]) | |
def sieve(n): | |
prime = [True for i in range(n+1)] | |
prime[0] = False | |
prime[1] = False | |
p = 2 | |
while (p * p <= n): | |
if (prime[p] == True): | |
for i in range(p * p, n+1, p): | |
prime[i] = False | |
p += 1 | |
return [idx for idx, p in enumerate(prime) if p] | |
primes = sieve(n) | |
getcontext().prec = n | |
b = Decimal(0) | |
pp = Decimal(1) | |
for p in primes: | |
b += Decimal(p - 1) / pp | |
pp *= p | |
print(b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment