0

After hours of Googling, I'm still at a standstill. I would appreciate if someone would point out the error in my formula or coding choice. Please keep in mind I'm new to Swift. I'm not used to non C-style for loops.

 if textField.text != "" { input = Double(textField.text!)! // parse input // return if number less than 2 entered if input < 2 { resultLabel.text = "Enter a number greater than or equal to 2." return; } // get square root of input and parse to int inputSquared = Int(sqrt(input)); // loop from 2 to input iterating by 1 for i in stride(from: 2, through: input, by: 1) { if inputSquared % Int(i) == 0 { resultLabel.text = "\(Int(input)) is not a prime number." } else { resultLabel.text = "\(Int(input)) is a prime number!" } } } 

I didn't know the formula on how to find a prime number. After looking up multiple formulas I have sorta settled on this one. Every result is a prime number, however. So my if condition is wrong. I just don't know how to fix it.

4
  • 1
    You should stride through: Int(sqrt(input)), and check input % i Commented Feb 27, 2017 at 6:16
  • 1
    I think should be sqrt(input).rounded(.up) @jtbandes Commented Feb 27, 2017 at 6:21
  • @jtbandes This does make my code more efficient. Thank you. I had input % i there because the var i was being made into a double automatically, giving me the error you cannot do modular with an int and a double. Still, though, my formula is incorrect. Should I be using a return statement in my if statement? Commented Feb 27, 2017 at 6:22
  • @ĐàoMinhHạt In another example I saw, someone was using floor(sqrt(input)). However, it kept the variable a double. So I cut out floor and just parsed it as an int. Commented Feb 27, 2017 at 6:24

3 Answers 3

1

Check my algorithm.It works.But I'm not sure this is an effective algorithm for prime number

 var input:Int = 30 var isPrime:Bool = true if(input == 2){ print("Input value 2 is prim number") } else if(input < 2){ print("Input value must greater than 2") } else{ for i in 2...input-1{ if((input%i) == 0){ isPrime = false break; } } if(isPrime){ print("Your Input Value \(input) is Prime!") } } 
Sign up to request clarification or add additional context in comments.

3 Comments

I tried bringing what you answered into a playground. It says that your for loop is written incorrectly. I'm not familiar enough with for loops to know how to correctly write it. Binary operator '...' cannot be applied to operands of type int and double
It seems your input value is Double Data Type.In my example,I have defined my input value as interger.So my loop is working properly.
You are correct and this worked! The problem is that I was getting the square root of the input. For a lot of the formulas I was seeing, people were doing this. I just used math.com's prime number calculator to test a few inputs to see if this is correct, and it worked!
1

A couple of solutions that work have been posted, but none of them explain why yours doesn't. Some of the comments get close, however.

Your basic problem is that you take the square root of input, then iterate from 2 to the input checking if the integer part of the square root is divisible by i. You got that the wrong way round. You need to iterate from 2 to the square root and check that the input is divisible by i. If it is, you stop because input is not prime. If you get to the end without finding a divisor, you have a prime.

Comments

-1

try this code in playground you will get this better idea and try to use playground when you try the swift as you are not familiar with swift playground is best.

let input = 13 // add your code that take value from textfield var prime = 1 // throw error less than 2 entered if input < 2 { assertionFailure("number should be 2 or greater") } // loop from 2 to input iterating by 1 for i in stride(from: 2, to: input, by: 1) { if input % i == 0{ prime = 0 } } if prime == 1 { print("\(input) number is prime") } else { print("\(input) number is not prime") } 

2 Comments

Yup, this works! I was getting the square root of the input before. That is what was messing me up. I don't know what assertionFailure is, and I would get in trouble if I used code I wasn't taught yet myself. If the future, though, I will make sure to learn more about it. Thank you for your help, @Nikunj Damani
sure you should try to explore the swift :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.