My code in Swift vs. Java Swift gives an error, but Java does not. Are there any differences?

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!

0
source share
3 answers

No, the apple did not get rid of for in cycles.

Your problem is the line:

  for l in 2...i-1 { 

Because if i-1 less than 2, this error occurs. So you need to check if i-1 is equal to or greater than 2. Check this code to prove the error:

 for l in 2...2 { //No error for l in 2...1 { //error 

So, I would do something like this if you want to save your code:

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

Also you have an error:

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

You need to either wrap me in println(i) or delete it.

+4
source

You should not compare if the Boolean type is == true. if (isPrimeFactor == true) is redundant.

  if isPrimeFactor { ... } else {...} 

You can create a read-only computed property to return a Bool value indicating whether Number Prime is or not the following:

 extension Int { var isPrime:Bool{ if self < 2 { return false } let squareRoot = Int(sqrt(Double(self))) if squareRoot * squareRoot == self { return false } for i in 2..<Int(ceil(sqrt(Double(self)))) { if self % i == 0 { return false } } return true } } 1.isPrime // false 2.isPrime // true 3.isPrime // true 4.isPrime // false 5.isPrime // true 6.isPrime // false 7.isPrime // true 8.isPrime // false 9.isPrime // false 10.isPrime // false 11.isPrime // true let myInt = 7 if myInt.isPrime { // do this } else { // do that } 

Apple has not gotten rid of ... You must write:

 for index in 0..<whatever { } 

or

 for index in 0...whatever { } 
+1
source

You should use 2 ..< i instead of 2 ... i - 1 .

2 ..< 2 can generate an empty sequence, while 2 ... 1 causes an error.

And you can simplify such code.

 OUTER: for i in 2 ... (num / 2) { if (num % i) == 0 { for l in 2 ..< i { if (i % l) == 0 { continue OUTER } } println(i) } } 
0
source

Source: https://habr.com/ru/post/1239490/


All Articles