简体   繁体   中英

Rewriting code to generate all numbers containing consecutive primes starting at two, less than a given number

I have been trying to solve this project euler problem, and have found a correct solution. My code however is terrible. I use several nested loops that I wish to make into some nicer functions or such. The following is the first three sections of the code, out of 10 sections required to solve the problem. Each has one more nested loop, and while the time is not an issue I would like to improve this code, but I am not sure how to implement this algorithm in a more concise manner.

What the nth section does is generate all numbers less than 1e9, that contain only the first n prime numbers(and then add numbers relating to the problem to a set).

I have tried for example to have a list of exponents for all primes, and incrementing the outermost nonzero exponent, and then backtracking when the product is larger than 1e9, however I have not been able to do anything successful.

If this question is not appropriate for this site I can delete it.

pseudofortunate=set()

pr=generate_primes(24)




num1=1
while num1<1e9/2:
    num1*=pr[0]
    num2=num1
    while num2<1e9/3:
        num2*=pr[1]
        m=num2+3
        while True:
            if is_prime(m):
                pseudofortunate.add(m-num2)
                break
            m+=2


num1=1
while num1<1e9/2:
    num1*=pr[0]
    num2=num1
    while num2<1e9/3:
        num2*=pr[1]
        num3=num2
        while num3<1e9/5:
            num3*=pr[2]
            m=num3+3
            while True:
                if is_prime(m):
                    pseudofortunate.add(m-num3)
                    break
                m+=2


num1=1
while num1<1e9/2:
    num1*=pr[0]
    num2=num1
    while num2<1e9/3:
        num2*=pr[1]
        num3=num2
        while num3<1e9/5:
            num3*=pr[2]
            num4=num3
            while num4<1e9/7:
                num4*=pr[3]
                m=num4+3

                while True:
                    if is_prime(m):
                        pseudofortunate.add(m-num4)
                        break
                    m+=2

this works to find the correct number of primes < 100. You'll need to change 100 to a variable and change numPrimes to actually set the prime value but it works:

int i;
int j;
int numPrimes = 0; 

for (i = 3;  i < 100; i++) {
    for (j = 2; j < (i-1); j++) {
        if ((i % j) == 0) {
            break;
        }
    }
    if (j == (i-1)) {
        ++numPrimes;
    }
}
// now add 1 for the value 2
numPrimes = ++numPrimes;

Ok so I figured this one out, by doing the nested loops recursively as a generator. Below is a MUCH nicer code that yields the same answer.

def loop(n,depth):
    if depth==0:
        num1=1
        while num1<n/2:
            num1*=pr[0]
            yield num1
    else:
        for i in loop(n,depth-1):
            num_depth=i
            while num_depth<n/pr[depth]:
                num_depth*=pr[depth]
                yield num_depth

def amicablenums(n=1e9):
    p=set()
    for i in range(len(pr)):
        a=set(loop(n,i))
        p=p|a
    return p


def pseudofortunate(n):
    m=n+3
    while True:
        if is_prime(m):
            return m-n
        m+=2
pr=generate_primes(24)
pseudofortunates=set()      
for i in amicablenums():
    pseudofortunates.add(pseudofortunate(i))
print sum(pseudofortunates)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM