Last active
February 8, 2016 14:50
-
-
Save carlynorama/0beee48cc3f2c09a3efb to your computer and use it in GitHub Desktop.
PrimeCheck Swift App Code. Has text field as first responder and dismiss on tap.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // ViewController.swift | |
| // PrimeChecker | |
| // | |
| // 2/7/16. | |
| // CC BY carlynorama. | |
| // | |
| import UIKit | |
| class ViewController: UIViewController { | |
| @IBOutlet weak var numberTextField: UITextField! | |
| @IBOutlet weak var resultMessage: UILabel! | |
| @IBAction func submitButton(sender: UIButton) { | |
| if let numberToCheck = Int(numberTextField.text!) { | |
| if isPrime(numberToCheck) { | |
| resultMessage.text = "PRRRIIIIME!" | |
| } else { | |
| resultMessage.text = "not prime" | |
| } | |
| } else { | |
| resultMessage.text = "Please enter a postive integer." | |
| } | |
| } | |
| func isEven(n: Int) -> Bool { return n % 2 == 0 } | |
| func isDivisableByThree(n: Int) -> Bool { return n % 3 == 0 } | |
| func isDivisableByFive(n: Int) -> Bool { return n % 5 == 0 } | |
| func sixKplus1(n: Int) -> Bool { return (n-1) % 6 == 0 } | |
| func sixKminus1(n: Int) -> Bool { return (n+1) % 6 == 0 } | |
| func possibleFactors(n: Int) -> Array<Int> { | |
| var intArray: [Int] = Array(0...Int(sqrt(Double(n)))) | |
| for (i, _) in intArray.enumerate().reverse(){ | |
| switch intArray[i]{ | |
| case Int.min..<3: | |
| intArray.removeAtIndex(i) | |
| case let x where isEven(x): | |
| intArray.removeAtIndex(i) | |
| case let x where isDivisableByThree(x): | |
| intArray.removeAtIndex(i) | |
| case let x where isDivisableByFive(x): | |
| intArray.removeAtIndex(i) | |
| case let x where sixKminus1(x): | |
| continue | |
| case let x where sixKplus1(x): | |
| continue | |
| default: | |
| intArray.removeAtIndex(i) | |
| } | |
| } | |
| print(intArray) | |
| return intArray | |
| } | |
| func hasFactors(n: Int) -> Bool { | |
| let checkThese = possibleFactors(n) | |
| for (i, _) in checkThese.enumerate() { | |
| if n % checkThese[i] != 0 { | |
| continue | |
| } else { | |
| return true | |
| } | |
| } | |
| return false | |
| } | |
| func isPrime(checkMe: Int) ->Bool { | |
| switch checkMe { | |
| case Int.min..<1: | |
| print("1 or smaller") | |
| return false | |
| case 2,3: | |
| print("2 or 3") | |
| return true | |
| case let x where isEven(x): | |
| print("divisible by 2") | |
| return false | |
| case let x where isDivisableByThree(x): | |
| print("divisible by 3") | |
| return false | |
| case let x where isDivisableByFive(x): | |
| print("divisible by 5") | |
| return false | |
| case let x where hasFactors(x): | |
| print("long factors check") | |
| return false | |
| default: | |
| print("defaulted out") | |
| return true | |
| } | |
| } | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| numberTextField.becomeFirstResponder() | |
| // Do any additional setup after loading the view, typically from a nib. | |
| } | |
| override func didReceiveMemoryWarning() { | |
| super.didReceiveMemoryWarning() | |
| // Dispose of any resources that can be recreated. | |
| } | |
| override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){ | |
| view.endEditing(true) | |
| super.touchesBegan(touches, withEvent: event) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment