2
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.

シクシク素数列 (Kotlin)

Last updated at Posted at 2018-12-17
シクシク素数列 https://qiita.com/advent-calendar/2018/4949prime-series
Crystal https://qiita.com/cielavenir/items/4c95fdf14b7a247e51e0
Kuin https://qiita.com/cielavenir/items/60993a2b2885690a3a4a
Swift https://qiita.com/cielavenir/items/db4c93532a53bfa33ac2
Kotlin https://qiita.com/cielavenir/items/f3ccdd5ebf245aba417c
Pascal https://qiita.com/cielavenir/items/4c962db6825d9a5eb511
4949.kt
fun isqrt(n:Int):Int{
	if(n<=0)return 0
	if(n<4)return 1
	var x=0
	var y=n
	while(x!=y&&x+1!=y){x=y;y=(n/y+y)/2}
	return x
}

fun isPrime(i:Int):Boolean{
	if(i<2){
		return false
	}
	for(j in 2..isqrt(i)){
		if(i%j<1){
			return false
		}
	}
	return true
}

fun is4949(i:Int):Boolean{
	if(i<=0)return false
	if(i%10==4||i%10==9)return true
	return is4949(i/10)
}

fun main(args: Array<String>){
	val n=readLine()!!.toInt()
	var i=2
	var cnt=0
	while(cnt<n){
		if(is4949(i)&&isPrime(i)){
			print(i)
			if(cnt<n-1){
				print(",")
			}else{
				println()
			}
			cnt+=1
		}
		i+=1
	}
}
2
0
1

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
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?