Не могу разобраться с простой задачей

swift

#1
6. First, write the following function:

func isNumberDivisible(_ number: Int, by divisor: Int) -> Bool

You’ll use this to determine if one number is divisible by another. It should return true when number is divisible by divisor.

Hint: You can use the modulo (%) operator to help you out here.

Next, write the main function:

func isPrime(_ number: Int) -> Bool

This should return true if number is prime, and false otherwise. A number is prime if it’s only divisible by 1 and itself.
You should loop through the numbers from 1 to the number and find the number’s divisors.
If it has any divisors other than 1 and itself, then the number isn’t prime. You’ll need to use the isNumberDivisible(_:by:) function you wrote earlier.
Use this function to check the following cases:

isPrime(6) // false
isPrime(13) // true
isPrime(8893) // true

Hint 1: Numbers less than 0 should not be considered prime. Check for this case at the start of the function and return early if the number is less than 0.
Hint 2: Use a for loop to find divisors. If you start at 2 and end before the number itself, then as soon as you find a divisor, you can return false.
*/


#2

Что конкретно у вас не получается разобрать?


#3

первая функция бро

func isNumberDivisible(_ number: Int, by divisor: Int) -> Bool {
divisor == 0 ? false : (number % divisor == 0 ? true : false)
}

вторая функция бро

func isPrime(_ number: Int) -> Bool {
if number < 0 {
return false
}

for nextNumber in 2..<number {
    if isNumberDivisible(number, by: nextNumber) {
        return false
    }
}
return true

}


#4

спасибо за ответ, функция написана через чур заумно для моего понимания еще) надо тот же функционал, но более простым языком))


#5

так а че там писать, держи

func isNumberDivisible(_ number: Int, by divisor: Int) -> Bool {
if divisor == 0 {
return false
} else {
if number % divisor == 0 {
return true
} else {
return false
}
}
}

первый вариант компактнее и красивее. :fist:


#6

спасибо)) может ты еще и в bitbucket шаришь ?)))


#7

Это выглядит как
if true { return true } else { return false }

Достаточно:

divisor == 0 ? false : number % divisor == 0