So, I am writing an application that will find the main factors of the number. I am writing an application in Swift, but this gives me an error if I set "num" to an even number, but not to an odd number. The error says, Execution was interrupted, reason: EXC_BAD_INSTRUCTION code=EXC_l386_INVOP, subcode=0x0 in 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 (which I thought was an exact copy of 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); } } }
In addition, Apple got rid of loops .. in for? I also get an error message.
EDIT: Wow. I realized that my code is just needed .. <instead of ... num-1 to work. Thanks for all the help to everyone who contributed!
source share