Question

I want to multiply an integer with all the numbers in a list I generate until a value of 65 is reached. I am starting off at 2(first prime) and multiply up to 63(again all primes) until I reach 65. If a value is not reached with 2 I then want it to try with 3, and so on until the correct value us reached. I am doing this in python, which I am new too aswell so I apologise if this is basic. I then want to print out the numbers which multiplied together to give me that value, i.e. I know 5 and 13 would give me 65. Here is some of my code below:

from __future__ import division
import fractions
ml = []
nl = []
p = 5
q = 17
d = 0
x = 2
y = 2

z = (p-1)*(q-1)
print z
n = p*q
print n

for x in range(z):
    if (fractions.gcd(x, z) == 1):
        ml.append(x)
    ##print ml

s = 1
for x in ml:
    t = s * x 
    if t == 65:
        print s
        print x
        break
    else:
        s = s + 1
Était-ce utile?

La solution

You're incrementing s and moving to the next element in ml in each stage of the loop. So you only try 1 * ml[0], 2 * ml[1], etc. I think you want two nested for loops, so that you try every element of ml with every possible value of s. You can get this behaviour a bit more cleanly with itertools:

for s,x in itertools.product(range(65),ml):
    t = s * x 
    if t == 65:
        print s
        print x
        break
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top