Finding Prime Number

OVERVIEW

We will be looking for Prime Number by typing the number that we think is Prime Number. But what really is a prime number? Prime number is only divisible by itself and 1 which includes 2, 3, 5, 7, 11 and so on.

Coding Section

What we will be doing is to create a function and determine if the number is less than 1 then it’s not prime. And if its more than 2, we’ll try to divide the number and if there is a decimal of 0.5 we know that it is prime.

func prime(number: Double) {
    if number <= 1 {
        print ("Not Prime")
    }
    
    if number >= 2 {
        var dividedBy2 = number / 2                   // 6.5
        var toBeConverted = dividedBy2 * 10           // 65
        var total = Int(toBeConverted) % 10           // 5
        if total == 5 {
            print("Prime")
        } else {
            print("Not Prime")
        }
        
    }
}
prime(number:13)

 

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*