Feb-06-2025, 08:51 PM
Hi.
First, I'm not a Python programmer so please escuse my ignorance.
I'm looking for help to improve this code.
It generate prime pairs that sum to even integers.
If you want to know the hows|whys of its design read this paper I just released.
Proof of Goldbach's Conjecture and Bertrand's Postulate Using Prime Generator Theory (GPT)
https://www.academia.edu/127404211/Proof...heory_PGT_
Here's the Ruby code from the paper.
1) It's way slower than the Ruby version.
2) It doesn't take input directly from the command line following the method
3) It doesn't take input numbers using underscores: 123_456_780
Here a timing difference example.
First, I'm not a Python programmer so please escuse my ignorance.
I'm looking for help to improve this code.
It generate prime pairs that sum to even integers.
If you want to know the hows|whys of its design read this paper I just released.
Proof of Goldbach's Conjecture and Bertrand's Postulate Using Prime Generator Theory (GPT)
https://www.academia.edu/127404211/Proof...heory_PGT_
Here's the Ruby code from the paper.
# Enable YJIT if using CRuby >= 3.3"
RubyVM::YJIT.enable if RUBY_ENGINE == "ruby" and RUBY_VERSION.to_f >= 3.3
def prime_pairs_lohi(n)
return puts "Input not even n > 2" unless n.even? && n > 2
return (pp [n, 1]; pp [n/2, n/2]; pp [n/2, n/2]) if n <= 6
# generate the low-half-residues (lhr) r < n/2
lhr = 3.step(n/2, 2).select { |r| r if r.gcd(n) == 1 }
ndiv2, rhi = n/2, n-2 # lhr:hhr midpoint, max residue limit
lhr_mults = [] # for lhr values not part of a pcp
# store all the powers of the lhr members < n-2
lhr.each do |r| # step thru the lhr members
r_pwr = r # set to first power of r
break if r > rhi / r_pwr # exit if r^2 > n-2, as all others are too
while r < rhi / r_pwr # while r^e < n-2
lhr_mults << (r_pwr *= r) # store its current power of r
end
end
# store all the cross-products of the lhr members < n-2
lhr_dup = lhr.dup # make copy of the lhr members list
while (r = lhr_dup.shift) && !lhr_dup.empty? # do mults of 1st list r w/others
ri_max = rhi / r # ri can't multiply r with values > this
break if lhr_dup[0] > ri_max # exit if product of consecutive r’s > n-2
lhr_dup.each do |ri| # for each residue in reduced list
break if ri > ri_max # exit for r if cross-product with ri > n-2
lhr_mults << r * ri # store value if < n-2
end # check cross-products of next lhr member
end
# convert lhr_mults vals > n/2 to their lhr complements n-r,
# store them, those < n/2, in lhr_del; it now holds non-pcp lhr vals
lhr_del = lhr_mults.map { |r_del| (r_del > ndiv2 ? n - r_del : r_del) }
pp [n, (lhr -= lhr_del).size] # show n and pcp prime pairs count
pp [lhr.first, n-lhr.first] # show first pcp prime pair of n
pp [lhr.last, n-lhr.last] # show last pcp prime pair of n
end
def tm; t = Time.now; yield; Time.now - t end # to time runtime execution
n = ARGV[0].to_i # get n value from terminal
puts tm { prime_pairs_lohi(n) } # show execution runtime as last outputHere's my Python3 translation.import time
from math import gcd
def prime_pairs_lohi(n):
if n&1 == 1 or n < 4: return print("Input not even n > 2")
if n <= 6: print([n, 1]); print([n/2, n/2]); print([n/2, n/2]); return
# generate the low-half-residues (lhr) r < n/2
lhr = [] # lhr (low half residues) of n
ndiv2, rhi = n//2, n-2 # lhr:hhr midpoint, max residue limit
for r in range(3, ndiv2, 2):
if gcd(r, n) == 1: lhr.append(r)
# store all the powers of the lhr members < n-2
lhr_mults = [] # lhr multiples, not part of a pcp
for r in lhr: # step thru the lhr members
r_pwr = r # set to first power of r
if r > rhi // r_pwr: break # exit if r^2 > n-2, as all others are too
while r < rhi // r_pwr: # while r^e < n-2
r_pwr *= r # increase power of r
lhr_mults.append(r_pwr) # store its current power of r
# store all the cross-products of the lhr members < n-2
lhr_dup = lhr.copy() # make copy of the lhr members list
while True: # do mults of 1st list r w/others
r = lhr_dup.pop(0) # get first lhr_dup element, reduce it by 1
if len(lhr_dup) == 0: break # exit if lhr_dup empty
ri_max = rhi // r # ri can't multiply r with values > this
if lhr_dup[0] > ri_max: break # exit if product of consecutive r’s > n-2
for ri in lhr_dup: # for each residue in reduced list
if ri > ri_max: break # exit for r if cross-product with ri > n-2
lhr_mults.append(r * ri) # store value if < n-2
# convert lhr_mults vals > n/2 to their lhr complements n-r,
# store them, those < n/2, in lhr_del; it now holds non-pcp lhr vals
for r in lhr_mults:
i = r if r < ndiv2 else n-r
if i in lhr: lhr.remove(i)
print([n, len(lhr)]) # show n and pcp prime pairs count
print([lhr[0], n-lhr[0]]) # show first pcp prime pair of n
print([lhr[-1], n-lhr[-1]]) # show last pcp prime pair of n
n = int(input()) # get n value from terminal
t1 = time.time()
prime_pairs_lohi(n)
print(time.time() - t1)It computes correct answers but:1) It's way slower than the Ruby version.
2) It doesn't take input directly from the command line following the method
3) It doesn't take input numbers using underscores: 123_456_780
Here a timing difference example.
➜ ~ ruby prime_pairs_lohi.rb 1_000_000 [1000000, 5402] [17, 999983] [499943, 500057] 0.13848254 ➜ ~ python3 prime_pairs_lohi.py 1000000 [1000000, 5402] [17, 999983] [499943, 500057] 222.1770532131195So I'm asking help to improve performance and make entering numbers the same as Ruby.
