1
0

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.

Swiftで素数判定してみたVol.1

Posted at

#Swiftで素数判定してみた

この記事ではSwift4.0を使用して記述します。

徐々に実行スピードを速くするなど改良していきたいと思います。

sosu.swift
import Foundation
    
func issosu(_ n:Int)->Bool{
    
    if(n<2){
	
        return false
		
    }
	
    if(n == 2){
	
        return true
		
    }
    
    var p = 3
	
    while p < Int(Double(n))+1 {
	
        if(n%p==0 && n != p){
		
		    return false
			
		}
		
        p = p + 2
		
    }
	
    return true
	
}
    
    
    
var n = 2

while n < 10000 {

    if issosu(n) {
	
        print(n)
		
    }
	
    if n == 2 {
	
        n = n - 1
		
    }
	
        n = n + 2
		
}
1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?