mercredi 25 février 2015

My code in Swift vs. Java. Swift gives an error but Java doesn't. Are there any differences?



So I'm writing an app that will find the prime factors of a number. I'm writing the app in Swift but it gives me an error if i set "num" to an even number but not an odd number. The error says, Execution was interrupted, reason: EXC_BAD_INSTRUCTION code=EXC_l386_INVOP, subcode=0x0 on the 5th line of code.


Here's the Swift code:



var num = 16

for i in 2...(num/2)-1 {

if ((num % i) == 0) {
var isPrimeFactor = true

for l in 2...i-1 {
if ((i%l) == 0) {
isPrimeFactor = false;
}//end if
}//end for

if (isPrimeFactor == true) {
i
}//end if

}//end if

}//end for


Here's the Java code(that I thought was an exact copy of the Swift code):



int num = 16;

for (int i=2; i<num/2; i++) {
if (num%i == 0) {
boolean isPrimeFactor = true;

for (int l=2; l<i; l++) {
if ((i%l) == 0) {
isPrimeFactor = false;
}
}

if (isPrimeFactor == true) {
System.out.println(i);
}
}
}


Also, did Apple get rid of the .. in for loops? I get an error using those too.




Aucun commentaire:

Enregistrer un commentaire