3

I wrote a code in C++ and would like to convert it to Python3. How to do that?

int N = 1000
for (p=2; p*p<=N; p++)
    for (w=p*p; w<=N; w=w+p)

I tried to change the first for loop to:

for p in range(2,N**0.5+1):

But it doesn't work - TypeError: 'float' object cannot be interpreted as an integer

And what about the second for loop?

2
  • 3
    try converting it to int.. like int(N**0.5) Commented Nov 15, 2017 at 15:33
  • 1
    range(2,int(N**0.5+1)): to be accurate Commented Nov 15, 2017 at 15:36

2 Answers 2

4

Try This ( in python You should do that with while ):

N = 1000
p = 2

while p*p <= N:

    p = p + 1
    w = p*p
    print(p)

    while w <= N:
        w = w + p
        print(w)

UPDATE :

The reason that you get the error "TypeError: 'float' object cannot be interpreted as an integer" is that the "range" function expects "integers" as arguments, whereas "N ** 0.5" is of type float, so is "N ** 0.5+1". To resolve this issue you can simply use "int" function to the conversion from float to integer. The following code is what you are looking for:

N = 1000
for p in range(2, int(N ** 0.5) + 1, 1):
    for w in range(p * p, N + 1, p):
        print(p, w)
Sign up to request clarification or add additional context in comments.

4 Comments

p = p + 1 => p += 1 and w = w + p => w += p
Is it possible to write for(w=p*p; w<=N; w=w+p) as a for loop in Python?
This while loop is not exactly the same as the loop in my question, it has one more execution so I have an error list assignment index out of range
The first p and w in this variant are effectively 3 and 9 instead of 2 and 4. Either increment p after the inner while loop or start p at 1 before the outer.
1
int N = 1000
for (p=2; p*p<=N; p++)
    for (w=p*p; w<=N; w=w+p)

In Python, the range() function takes in 3 parameters: int start, int stop, int step. Since your second argument is N**0.5, with 0.5 of type float, the result of that calculation is interpreted as of type float.

My suggestion is:

from math import sqrt
n = 1000
m = int(sqrt(n))
for p in range(2,m):
  for w in range(p*p, n+1, p)
    print(p,w)

Does that solve your problem?

3 Comments

Nice! so you basically copied my answer and replaced "**0.5" with the "sqrt" function? which also needs importing the "math" module unnecessarily (import math can be avoided if you simply use "**0.5"). Could you please elaborate apart from using the sqrt function instead of "**0.5" how your answer is different from mine?
@DRPK This problem is not that difficult, I don't have the comment privilege at the time and wanted to explain it a little bit more, didn't mean to copy you in any way
It's almost the same as DRPK answer. You forgot m+1 and a colon in the 2nd for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.