私たちが知っているように、それはすべての方法でカメです。しかし、それはすべての方法でも素数ですか?
それが次の条件を満たす場合、数字は「カメの素数」とみなされます。
1) It is prime.
2) It is possible to remove a single digit leaving a prime number.
3) Step 2 can be repeated until left with a single digit prime.
たとえば、 23
はです。 2
または 3
コード>、どちらもプライムです。また、 29
、次に 2
に減らすこともできます。 15
(素数ではない)、 51
(素数でない)、または
11
11
は素数ですが、 1
にしか縮小できません。
正の整数が与えられた場合、それが「カメの一種」であるかどうかを判定します。真実または偽の値に対して同じ出力を出す限り、出力はどのような形式でも構いません。
テストケース:
input -> output
1 -> false
2 -> true
17 -> true
19 -> false
239 -> true
389 -> false
スコアリング
This is code-golf,
so the shortest answer in each language wins!
Haskell, 104 102 99
98 97 95 91 bytes
p x=product[2..x-1]^2`mod`x>0
f[]=1>0
f x|y<-zip[0..]x=or[f[snd b|b<-y,b/=a]|a<-y,p$read x]
説明
まず、素数性テストを設定します
p x=product[2..x-1]^2`mod`x>0
これは、ウィルソンの定理を使用して入力の素数を決定します。
次に、空の文字列が真実であると主張する基本ケースを宣言します。
f[]=1>0
今度は実際の関数を定義する
f x|y<-zip[0..]x=or[f[snd b|b<-y,b/=a]|a<-y,p$read x]
パターンガードを使用して、 y
に zip [0 ..] x
をバインドします。次に、答えは
or[f[snd b|b<-y,b/=a]|a<-y,p$read x]
[[snd b|b<-y,b/=a]|a<-y]
is all of the
numbers that are a digit removed from our input. So we are
asserting that at least one of these numbers is truthy for
f
. In order to ensure that composite numbers are falsy
we add in prime$read x
. If the number is not prime the
list will become empty and the any
of a empty list is
false.