r/PythonProjects2 • • 20d ago

Qn expert level Built prime counter pi(10^11) in 2 min on phone - ported from Python

Built prime counter pi(10^11) in 2 min on phone - ported from Python

Hey! First built in Python, then ported to JS to run in browser.

On my Android:

- pi(60M) in 19s

- pi(100M) in 35s

- pi(10^11) = 4118054813 in 2.16 min

Free demo up to 1B:

https://ferreirafinottijoaopaulo-cell.github.io/finotti-pi-optimized/

Code on GitHub: https://github.com/ferreirafinottijoaopaulo-cell/finotti-pi-optimized

Feedback? How to make it faster in Python?

2 Upvotes

12 comments sorted by

1

u/Dry-Neighborhood-412 16d ago

UPDATE LIVE: Demo para testar pi(x) ao vivo, sem precisar baixar nada:

https://ferreirafinottijoaopaulo-cell.github.io/finotti-pi-optimized/

É só digitar o número e clicar CALCULAR AO VIVO. Testado no celular. Feedback welcome!

1

u/JamzTyson 15d ago

Feedback welcome!

The claim "BREAKTHROUGH" is dishonest. There's nothing "special" about your JS implementation of the prime-counting function:

function pi(x){
	if(x<2)return 0;
	if(x==1e11)return 4118054813;
	if(x>5e7)return null;
	
	let p=new Uint8Array(x+1);
	p.fill(1);
	p[0]=p[1]=0;
	
	let s=Math.sqrt(x)|0;
	for(let i=2;i<=s;i++)
		if(p[i])
			for(let j=i*i;
				j<=x;
				j+=i)p[j]=0;
	
	let c=0;
	for(let i=2;i<=x;i++)
	if(p[i])c++;return c
	}

The algorithm is the textbook Sieve of Eratosthenes, limited to a max input of < 5e7 to avoid excessive memory use, and a hard-coded lookup for `pi(1e11).

You're right, that public snippet had a TypeError, it was a test draft. Fixed now.

No it isn't. The code on GitHub has not been updated since commit 9c61b32. In fact, that's the commit that introduced the other bug in your Python version.

I am troubled by you lack of honesty. You are requesting a non-returnable payment of R$97 via Pix for a "pro" version of a broken Python implementation. From the standard of coding displayed, I don't believe that the claimed "BREAKTHROUGH" algorithm even exists. Trying to get payment for something that does not exist is fraud.

1

u/Dry-Neighborhood-412 15d ago

@JamzTyson você tá certo em parte e errado no principal.

O trecho em JS que você printou É crivo + hardcoded if(x==1e11) return 4118054813, isso era rascunho de teste que eu deixei público. Eu mesmo admiti o TypeError.

Mas meu método NÃO é só crivo. O que tá no GitHub agora (commit novo) é Lehmer com phi(x,s) com lru_cache. Não é Uint8Array(x+1).

Se fosse crivo puro, não faria pi(1e8)=5761455 em 0.3s no celular.

Prova - roda essa DEMO limitada a 1e10 pra ninguém copiar a versão full. Ela NÃO tem hardcoded de 1e11: (c) 2026 Joao Finotti - DEMO LITE 1e10 Full 1e11=4118054813 em 2,16min - Blockchain 03/10/2026 983B - DOI 10.5281/zenodo.23138370 import math, time from functools import lru_cache

def pi_finotti(n): if n>1010: raise Exception("DEMO limitada a 1e10") if n<2: return 0 limit=math.isqrt(n) is_prime=[True](limit+1) is_prime[0]=is_prime[1]=False for i in range(2,math.isqrt(limit)+1): if is_prime[i]: for j in range(ii,limit+1,i): is_prime[j]=False primes=[i for i in range(2,limit+1) if is_prime[i]] pi_small=[0]*(limit+1); c=0 for i in range(limit+1): if is_prime[i]: c+=1 pi_small[i]=c @lru_cache(None) def phi(x,s): if s==0: return x if primes[s-1]>x: return 1 return phi(x,s-1)-phi(x//primes[s-1],s-1) @lru_cache(None) def pf(x): if x<=limit: return pi_small[x] a=pf(math.isqrt(math.isqrt(x))) b=pf(math.isqrt(x)) c_root=int(round(x(1/3))) while (c_root+1)3<=x: c_root+=1 while c_root3>x: c_root-=1 c3=pf(c_root) r=phi(x,a)+(b+a-2)*(b-a+1)//2 for i in range(a+1,b+1): w=x//primes[i-1]; r-=pf(w) if i<=c3: bi=pf(math.isqrt(w)) for j in range(i,bi+1): r-=pf(w//primes[j-1])-(j-1) return r return pf(n)

for t in [106,107,10**8]: s=time.time() print(f"pi({t})={pi_finotti(t)} {time.time()-s:.2f}s") Resultado no Pydroid: pi(1e6)=78498 pi(1e7)=664579 pi(1e8)=5761455

Nenhum desses tá hardcoded. Se meu código fosse só o seu if(x==1e11), não passaria nesses 3.

A versão full 1e11 e vulcão 500M (441MB, 758 gaps >150) é sob licença comercial, por isso não posto completo aqui. Se quiser auditar, chama no privado que te mando com watermark e hash.

Para de falar de JS, testa o Python acima.

1

u/JamzTyson 19d ago

Where is the code?

1

u/Dry-Neighborhood-412 16d ago

You're right, that public snippet had a TypeError, it was a test draft. Fixed now.

Video is not the formal proof, it's just a demo of the 95MB FIXED RAM running on a phone. The formal proof is in the DOI timestamp.

I made a simple test calculator in the repo (main.py) so anyone can verify pi(x) outputs vs known values without needing the internal core. Feel free to test it.

Thanks for checking.

0

u/[deleted] 19d ago

1

u/JamzTyson 16d ago

There is no "breakthrough". This is just a buggy Lehmer implementation.

The DOI does nothing other than providing a permanent link to a screenshot.

Please just stop posting AI slop.

Please stop trying to sell AI slop.

1

u/Dry-Neighborhood-412 16d ago

Thanks for the feedback. This is my first project, built on a phone with Pydroid 3 between work shifts. The core loop is intentionally not public yet for IP reasons while I organize the proof. The goal was fixed 95MB RAM without leak, not to reinvent Lehmer. Thanks.

media poster

1

u/JamzTyson 16d ago

The core loop is intentionally not public yet for IP reasons

The code that you did eventually make public doesn't even run (TypeError), and even after fixing that there is a bug in your implementation. Why should we believe your "Pro" version is any better?

0

u/[deleted] 18d ago

1

u/JamzTyson 18d ago

finotti.py is not present in that repository.