What is Python's equivalent of Java's standard for-loop? - java

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

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:
for Statements
xrange
all

A direct translation would be:
for i in range(3, int(math.sqrt(n)), 2):
if n % i == 0:
return False

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

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

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?

Related

Code optimization - BigInteger

The code works correctly until I give it a big value - it takes too much time to execute.
Can you give me some advice how to optimize it?
BigInteger type of n parameter is a must, it's a part of the task ;)
public static String oddity(BigInteger n) {
List<BigInteger> list = new ArrayList<BigInteger>();
String result = null;
for (BigInteger bi = BigInteger.valueOf(1);
bi.compareTo(n) <= 0;
bi = bi.add(BigInteger.ONE)) {
if (n.mod(bi).equals(BigInteger.ZERO))
list.add(bi);
}
if (list.size() % 2 == 0)
result = "even";
else result = "odd";
return result;
}
The purpose of this is to return 'odd' if the number of "n" divisors is odd. Otherwise return 'even'.
Thinking rather than just programming would help a lot. You don't need to find all divisors. You don't even need to count them. All you need is to find out if the count is odd.
But divisors always come in pairs: For every divisor i also n/i is a divisor.
So the count is always even, except when there's a divisor i equal to n/i. Use Guava sqrt ...
As you don't use the list except to get its final size, you could use an integer as a counter, i.e: do n++ instead of list.add(bi).
This is going to save huge amount of memory. Hence save time used to manage its allocation.
// Lambda
long counter = IntStream
.range(1, (int) Math.sqrt(n.longValue())+1)
.filter(i -> n.longValue() % i == 0 && n.longValue() / i == i)
.count();
return (counter % 2 == 0) ? "even" : "odd";
int counter = 0;
for (long i = 1; i <= Math.sqrt(n.longValue()); i++) {
if(n.longValue() % i == 0 && n.longValue()/ i == i){
counter++;
}
}
return (counter % 2 == 0) ? "even" : "odd";

Difference between Modulo in Java and VBScript

Is there a difference between the way that the Modulo operates calculates values in Java vs the way that it does in VBScript?
Both of these are returning a digit that I am using as part of a larger number later in the code but I believe the issue is between the way VBScript and Java are handling the Mod operator. I may be wrong though.
I am trying to work through reasons why I am seeing different outputs from when I run the below VBscript code block vs my replicated version in Java, sorry for the delay in updating the post.
The function takes in a String and then works to determine a return digit based upon the logic in the loop. The missing code just has to do with initializing the variables used, and determining the length of the string to loop over.
Any help would be greatly appreciated! Thank you
VBScript:
For i=1 to Length
CurrentNumber = Mid(CCNumber,i,1)
CurrentNumber = Int(CurrentNumber)
If (i mod 2) <> 0 then
ModNumber = CurrentNumber * 2
If ModNumber > 9 then
Total = Total + 1 + (ModNumber mod 10)
Else
Total = Total + ModNumber
End If
Else
Total = Total + CurrentNumber
End If
Next
cd = ((Int(Total/10) + 1) * 10) - Total
if cd = 10 then cd = 0
CheckDigit = cd
Java:
for (i=0; i<length; i++)
{
String currentNumberString = CCNumber.substring(i,i+1);
currentNumber = Integer.valueOf(currentNumberString);
if (i % 2 != 0)
{
Integer ModNumber = currentNumber * 2;
if (ModNumber > 9)
{
total = total + 1 + (ModNumber % 10);
}
else
{
total = total + ModNumber;
}
}
else
{
total = total + currentNumber;
}
}
int cd = ((Integer.valueOf(total/10) + 1) * 10) - total;
if (cd == 10)
{
cd = 0;
}
return cd;
}
One difference: The Mod operator in VBScript always returns an integer. Java's % operator can return a fractional value. So 5.2 Mod 2 evaluates to 1 in VBScript, but 5.2 % 2 evaluates to 1.2 in Java.
Edit: Based on your edit, this appears to be the Luhn algorithm. The only real problem with the Java code is a typo; nothing to do with the mod operator. Here you assign a variable currrentNumber (with a triple R):
currrentNumber = Integer.valueOf(currentNumberString);
Then you use a different variable (double R):
Integer ModNumber = currentNumber * 2;
Edit: Another difference is that because VBScript string indices start at 1, and Java string indices start at 0, the code is using different alternate digits. Either If (i mod 2) <> 0 should be If (i mod 2) = 0, or if (i % 2 != 0) should be if (i % 2 == 0), I'm not sure which.

python code not running right, same thing in java does

I was trying to solve Project Euler problem 10 using python, but my program gave a wrong result. Since I am a complete noob to python and I could not find any fault in my (apparently brute-force) logic, I wrote a program in java (almost translated it), and it gave a different result, which turned out to be right.
Here is the python code:
from math import *
limit = 2000000
def isPrime(number):
if number == 2: return 1
elif number % 2 == 0: return 0
elif number == 3: return 1
elif number == 5: return 1
elif number == 7: return 1
else:
rootOfNumber = sqrt(number)
tag = 3
while tag < rootOfNumber:
if number % tag != 0:
tag += 2
else:
break ###
if tag >= rootOfNumber: ###EDIT: it should by only tag > rootOfNumber here
return 1 ### Thats what the problem was.
else:
return 0
sum = 2 # 2 is an even prime, something we are not iterating for
for i in range(3, limit, 2):
if isPrime(i) == 1:
sum += i
print(sum)
print('done...')
The equivalent java code is:
public class Prob10{
static int limit = 2000000;
static long sum = 2L; // 2 is an even prime, something we are not iterating for
public static void main (String[] args) {
for(int i = 3; i < limit; i+=2) {
if( isPrime(i) )
sum += i;
}
System.out.println(sum);
}
private static boolean isPrime (int number) {
if (number == 2) return true;
else if (number == 3 || number == 5 || number == 7) return true;
else {
double rootOfNumber = Math.sqrt(number);
int tag = 3;
while (tag < rootOfNumber) {
if (number % tag != 0)
tag +=2;
else
break;
}
if (tag > rootOfNumber)
return true;
else
return false;
}
}
}
I think I am doing some silly mistake or missing some subtle point.
p.s. I know my isPrime implementation is not too good. I am not printing the outputs because it may spoil the problem for others.
Any comments about (bad) style in the python program are welcome.
Try running with your code for example isPrime(49). You should figure out your problem from there. You have replaced a > with a >= in if (tag > rootOfNumber)
.Also as some coding style, you could just replace the first lines with:
if i in (2, 3, 5, 7): return 1
elif number % 2 == 0: return 0
else:
......
After a quick skim it appears to me that this line in the Python version is superfluous and it might be the cause of the problem:
elif number % 2 == 0: return 0
Why don't you return False for return value of 0? That would make it more simple.

Most elegant way to change 0 to 1 and vice versa

What is the most elegant way to do the next stuff:
int i = oneOrZero;
if (i == 0) {
i = 1;
} else {
i = 0;
}
You can assume that i can have only 1 or 0 value.
i ^= 1;
XOR the value with 1. This gives you both ways (in case you need to flip 0 <--> 1 either way):
0 ^ 1 = 1
1 ^ 1 = 0
subtraction?
i = 1 - i;
i = (i == 0)?1:0 is one way, though I like #Jimmy's and #Yuval's versions better.
i = ( i + 1 ) % 2, though I think we all agree the subtraction or xor method is better! (Though it has the added benefit of "flipping the switch" for more than binary.)
Use Bitwise XOR operator:
i ^= 1;
int x = 0;
x=(int)Math.cos(x);
System.out.println("X value "+x);

Why doesn't this loop terminate?

Here's the sample code:
public static void col (int n)
{
if (n % 2 == 0)
n = n/2 ;
if (n % 2 != 0)
n = ((n*3)+1) ;
System.out.println (n) ;
if (n != 1)
col (n) ;
}
this works just fine until it gets down to 2. then it outputs 2 4 2 4 2 4 2 4 2 4 infinitely. it seems to me that if 2 is entered as n then (n % 2 == 0) is true 2 will be divided by 2 to yeild 1. then 1 will be printed and since (n != 1) is false the loop will terminate.
Why doesn't this happen?
Because when you get to 1, you are multiplying by 3 and adding 1, taking you back to 4.
You need an ELSE in there. I don't know java, but it would look something like:
public static void col (int n)
{
if (n % 2 == 0)
n = n/2 ;
else if (n % 2 != 0)
n = ((n*3)+1) ;
System.out.println (n) ;
if (n != 1)
col (n) ;
}
EDIT: as mentioned in the comments, you can omit the if test after the else:
if (n % 2 == 0)
n = n/2 ;
else
n = ((n*3)+1) ;
I think you'll have to change the 2nd if statement to an else
if (n % 2 == 0) // if the n is even
n = n/2 ;
else // if n is odd
n = ((n*3)+1) ;
The answer to the question can be read directly in the code:
Assume n is 2
(n % 2 == 0) is true therefore n <- 1
(n % 2 != 0) is also true therefore 4 <- n
this warrants a call to function with n = 4, which is then changed to 2 and
"back to square 1"
by replacing the second test by an else, you solve this logic problem, at the cost of possibly causing more recursion (since in the current logic, two operations are sometimes performed in one iteration). Such a fix will also solve a more subtle bug, which is that in the current version not all new values of n are printed out.
Now, for extra credit, prove that not matter the initial value of n, the number of recursions is finite (i.e. the sequence converges to 1). ;-)
Use if/then/else. Your logic is wrong.
when the input is 2:
if (n % 2 == 0) //true
n = n/2; //n = 1
if (n % 2 != 0) //true
n = ((n*3)+1); //n = 4
System.out.println (n); //prints 4
if (n != 1) //true
col (n); //call col(4)
Does it work if you change it to this?
if (n % 2 == 0)
n = n/2 ;
else if (n % 2 != 0)
n = ((n*3)+1) ;
It looks like you're getting 2, dividing by 2 to get 1, then checking to see if 1/2 has a remainder (it does), and multiplying it by 3 and adding 1, to get 4....
if (n % 2 != 0)
n = ((n*3)+1) ;
this code is again implemented whenever u get 1.
therefore the recursive function will be called repeatedly hence leading to an infinite rec calling and code will never terminate.
in addition to an else if to govern the condition that n is odd that same line also needs & n != 1 to be added to it within the conditional. So this:
else if (n % 2 != 0 & n != 1)

Categories