2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift3で素数判定

Last updated at Posted at 2017-09-27

数値を受け取り、入力された数が素数かどうかを判定。
約数が二つだけなのが素数。

->Void は省略可。


func isPrimeNumbers(_ number:Int)->Void{
    let num = number
    var yakusuu:Int = 0

    //divisor=割る数
    for divisor in 1...num{
        //余りがゼロならば
        if num % divisor == 0{
            yakusuu += 1
        }
    }
    //ループが終了した時の最終的な約数の数で一回判定すれば良い→for文の外に書く。
    if yakusuu == 2{
        print("素数です")
    }else {
        print("素数ではありません")
    }
}
isPrimeNumbers(2017)

2
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?