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.
through: Int(sqrt(input)), and checkinput % isqrt(input).rounded(.up)@jtbandesinput % ithere 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?floor(sqrt(input)). However, it kept the variable a double. So I cut outfloorand just parsed it as an int.