What is Python's equivalent of Java's standard for-loop?
Asked Answered
D

5

15

I'm writing a simple algorithm to check the primality of an integer and I'm having a problem translating this Java code into Python:

for (int i = 3; i < Math.sqrt(n); i += 2) {
    if (n % i == 0)
        return false;
}

So, I've been trying to use this, but I'm obviously skipping the division by 3:

i = 3
while (i < int(math.sqrt(n))):
    i += 2  # where do I put this?
    if (n % i == 0):
        return False
Dou answered 1/7, 2013 at 23:24 Comment(2)
for (a; b; c) { _ } -> a; while(b) { _; c; } - barring scoping issues and having to deal with continue. The last component of the for-each construct is evalutated after each evaluation of the loop body.Mayer
You might want to take a look at blog.startifact.com/posts/older/what-is-pythonic.html in reference to wanting to translate java (or other languages) to python - don't forget to be pythonic!Codeclination
T
24

The only for-loop in Python is technically a "for-each", so you can use something like

for i in xrange(3, int(math.sqrt(n)), 2):  # use 'range' in Python 3
    if n % i == 0:
        return False

Of course, Python can do better than that:

all(n % i for i in xrange(3, int(math.sqrt(n)), 2))

would be equivalent as well (assuming there's a return true at the end of that Java loop). Indeed, the latter would be considered the Pythonic way to approach it.


Reference:

Trueblood answered 1/7, 2013 at 23:26 Comment(3)
Python has while loops… Or am I misinterpreting part of your answer?Grower
@Grower I think he meant Python doesn't have the C-like for loop that merges an initializer, stop condition, and step statement. That is: the only for in Python is a foreach.Indispensable
Indeed, I clarified that statement.Trueblood
I
4

A direct translation would be:

for i in range(3, int(math.sqrt(n)), 2):
    if n % i == 0:
        return False
Inveigh answered 1/7, 2013 at 23:26 Comment(0)
G
3

In a Java for loop, the step (the i += 2 part in your example) occurs at the end of the loop, just before it repeats. Translated to a while, your for loop would be equivalent to:

int i = 3;
while (i < Math.sqrt(n)) {
    if (n % i == 0) {
        return false;
    }
    i += 2;
}

Which in Python is similar:

i = 3
while i < math.sqrt(n):
    if n % i == 0:
        return False
    i += 2

However, you can make this more "Pythonic" and easier to read by using Python's xrange function, which allows you to specify a step parameter:

for i in xrange(3, math.sqrt(n), 2):
    if n % i == 0:
        return False
Grower answered 1/7, 2013 at 23:30 Comment(0)
C
1

Use a basic Python for i in range loop:

for i in range(3, math.round(math.sqrt(x)), 2):
    if (n % i == 0):
        return false
Carpous answered 1/7, 2013 at 23:26 Comment(0)
M
0

I get this answer from an automatic translation by AgileUML:

def op(self, n) :
  result = False
  i = 0
  i = 3
  while i < math.sqrt(n) :
    if n % i == 0 :
      return False
    else :
      pass
    i = (i + 2)
  return True

It seems to be semantically correct?

Monophysite answered 26/10, 2022 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.