This is a tiny algorithm which generates primes which are spaced at a given ratio from one another. I try to avoid writing code here, but if I tried mathematical notation for this I'd end up causing even more confusion, so please bear with me:
n = 100 # number of primes to calculate
k = 1
ratio = 1/3 # relative increase from one of my prime results to the next
record = 1 # largest prime seen yet
primes = [] # list for results
for i in range(n):
v = lpf[ceiling[k*ratio]] # stores a prime in v
k += v # k keeps a running total of all values so far
if v > record: # if the prime returned is bigger than any other so far,
record = v # update accordingly
primes += [v] # and add that prime to the list of results
Here, lpf means least prime factor.
This code will return $n$ primes such that, if $a_i$ is the $i$th prime returned by the algorithm, $\frac{a_i}{a_{i+1}}$ approaches ratio + 1, which in the block above is $4/3$. The ratio can apparently be any value in $(0,1)$; if you make it low enough, it will simply return the first $n$ primes.
The example above would exit the loop with the list primes containing $\{2,3,5,7,11,17,23,31,41,59,79,107,149,199,269,\dots,8810551399451,11747401865963\}$, where the ratio between successive terms approaches $4/3$, becoming more accurate as it goes.
The question
I stumbled into this today while screwing around but can't see why it works. It's clear that lpf will only ever give you prime values, and since this code only takes the largest one seen yet, they'll be increasing primes, but I don't get the part where it's being forced to adhere to the ratio provided. So if this makes sense to someone else, do please share.
Here's working Mathematica code for it in case anyone wants that:
n=100;k=1;m=1;s={};
While[Length[s]<n,
t=FactorInteger[Ceiling[k/3]][[1,1]];
If[t>m,m=t;AppendTo[s,t]];
k+=t;
]
s
I see how this works now. When you have $$\textrm{lpf}\left(\left\lceil \frac{k}{ratio} \right\rceil\right)$$ hitting a largest-yet prime, the lpf function effectively copies that prime to be added to our count, which absent anything else would double the current value. Adding the ratio factor scales that down as expected.
Once that gets added, the very next value yielded is already roughly at the correct relative spot we'd be expecting for our next prime, and because most of the time the lpf function will cause very slow growth, and primes are plentiful, you effectively always end up hitting your next prime very near to the ratio you've specified. Rinse and repeat.