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.

合成数列の和Advent Calendar 2018

Day 12

合成数列の和 (Kuin)

Last updated at Posted at 2018-12-11
合成数列の和 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

Rubyから雑に移植したCrystalで枠を得るのははばかられる状況になったので、若干マイナーなところでKuin

くいなちゃんはcui版だけで良いのでマルチプラットフォームに早く汁☆

composite.kn
func isqrt(n: int): int
	if(n <= 0)
		ret 0
	end if
	if(n < 4)
		ret 1
	end if
	var x: int :: 0
	var y: int :: n
	while(x <> y & x + 1 <> y)
		do x :: y
		do y :: (n / y + y) / 2
	end while
	ret x
end func

func main()
	var n: int :: 0
	var cnt: int :: 0
	var i: int :: 3
	var s: int :: 0
	var _: bool
	do n :: cui@input().toInt(&_)
	while _composite(cnt < n)
		do i :: i + 1
		block _chkprime
			if(i < 2)
				break _chkprime
			end if
			for j(2, @isqrt(i))
				if(i % j < 1)
					break _chkprime
				end if
			end for
			; prime
			skip _composite
		end block
		; composite
		do cnt :: cnt + 1
		do s :: s + i
	end while
	do cui@print(s.toStr())
	do cui@print("\n")
end func
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?