0
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-12
合成数列の和 https://qiita.com/advent-calendar/2018/composite-number
Python(/Ruby) https://qiita.com/cielavenir/items/dcbd839b0c66af1bc971
Crystal https://qiita.com/cielavenir/items/1a0dd1a94aca9935f5aa
Kuin https://qiita.com/cielavenir/items/47fd18fc68055d990599
Swift https://qiita.com/cielavenir/items/6cde3b20866c842dc9e2
Kotlin https://qiita.com/cielavenir/items/f58eaa30f90781229a6d
Pascal https://qiita.com/cielavenir/items/35c2fc54ca16b620c9c7
composite.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 main(args: Array<String>){
	val n=readLine()!!.toInt()
	var i=2
	var cnt=0
	var s=0
	while(cnt<n){
		if(!isPrime(i)){
			s+=i
			cnt+=1
		}
		i+=1
	}
	println(s)
}

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