読者です 読者をやめる 読者になる 読者になる

mu chance or much chance ?

日々の戯れ言

Project Euler 35

Project Euler

Problem35を解き直しました.

  • Circular primes

The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?

https://projecteuler.net/problem=35

問題は「100万未満の巡回素数はいくつあるか求めよ」.

require 'prime'
prime = Prime.each(1000000).to_a
count = 0

for i in 0..prime.length - 1
	len = prime[i].to_s.length
	k = prime[i]
	if !Prime.prime?(k) then
		next
	end
	flag = false
	(len - 1).times do |i|
		keta = Math.log10(k).to_i
		amari10 = k % 10
		k = amari10 * (10 ** keta) + (k / 10)
		if !Prime.prime?(k) then
			flag = true
			break
		end
	end
	if !flag then
		count += 1
		p prime[i]
	end
end

p count

素数を求めて,その素数が巡回素数かどうかを判断します.