セミプライムですか?


26

驚くべきことに、数値が準素数であるかどうかを判断するための質問があるとは思わない。

半素数とは、2つの(必ずしも別個ではない)素数の積である自然数です。

十分にシンプルですが、非常に重要な概念です。

正の整数を指定して、それがセミプライムかどうかを判断します。出力は、trueまたはfalseの値に対して同じ出力を提供する限り、どのような形式でもかまいません。また、パフォーマンスやオーバーフローが問題にならないほど入力が適度に小さいと仮定することもできます。

テストケース:

input -> output
1     -> false
2     -> false
3     -> false
4     -> true
6     -> true
8     -> false
30    -> false   (5 * 3 * 2), note it must be EXACTLY 2 (non-distinct) primes
49    -> true    (7 * 7)      still technically 2 primes
95    -> true
25195908475657893494027183240048398571429282126204032027777137836043662020707595556264018525880784406918290641249515082189298559149176184502808489120072844992687392807287776735971418347270261896375014971824691165077613379859095700097330459748808428401797429100642458691817195118746121515172654632282216869987549182422433637259085141865462043576798423387184774447920739934236584823824281198163815010674810451660377306056201619676256133844143603833904414952634432190114657544454178424020924616515723350778707749817125772467962926386356373289912154831438167899885040445364023527381951378636564391212010397122822120720357
      -> true, and go call someone, you just cracked RSA-2048

これはなので、標準のルールが適用されます!


4
@WheatWizard 3つの素数を要求するという点で少し違いがあり(ほとんどすべての言語で大きな違いではない)、異なる素数のみである(一部の言語ではかなり異なる)。さらに詳細な議論を続けたい場合は、チャットで話し合うことができます。
ハイパーニュートリノ

2
@WheatWizardあなたは良い点を挙げますが、同様に、私たちはすでに多くのタイプの質問をたくさん持っています。私はそれが別の質問/投稿を正当化すると信じるでしょう。
ハイパーニュートリノ

2
@hyperneutrinoは両方の課題の答えを見て、違いはソースコードの2対3の単一の数字のように見えます。私はそれを大きな違いとはほとんど言いません。
小麦ウィザード

2
@WheatWizard「明確な」対「明確ではない」もあります
...-HyperNeutrino

3
@LordFarquaad重複しているからといって、それが悪いわけではありません。私の考えでは、重複するのは良いことです。それは、コミュニティが既に質問したほど面白いと思うものを求めていることを意味します。
小麦ウィザード

回答:


19

Brachylog、2 バイト

基本的に、Sphenic numberチャレンジに対するFatalizeの回答からの移植です。

ḋĊ

オンラインでお試しください!

どうやって?

ḋĊ - implicitly takes input
ḋ  - prime factorisation (with duplicates included)
 Ċ - is a couple

1
実際に仕事に
ふさわしい

2
@Uriel Ċは、実際には2つの変数の組み込みリストです。宣言型言語であるため、デフォルトでは、出力は満足度のテストになります(たとえば、それ自体がtrue.負でない整数に対して出力されます)。
ジョナサンアラン

この2バイトはどうですか?
ハロルド

1
@haroldヘッダーリンクにBrachylogのコードページへの「バイト」を作成するように更新しました。16進ダンプはになりますc6 eb
ジョナサンアラン


8

Mathematica、16バイト

PrimeOmega@#==2&

PrimeOmega 多重度をカウントして、素因数の数をカウントします。


1
ダン、ビルトインはありますか?
ジョンファンミン

1
@JungHwanMinあった場合のみSemiprimeQ
ngenisis

いいね 私は知りませんでしたPrimeOmega
DavidC


7

Python 3、54バイト

lambda n:0<sum((n%x<1)+(x**3==n)for x in range(2,n))<3

オンラインでお試しください!

以前versonは、大規模なキューブ番号にいくつかの丸めの問題(持っていた125343など)
それが持っている場合、これは、除数(だけではなく、素数)の量を計算し1たり2、それを返しますTrue
唯一の例外は、数に3つ以上の素因数があり、2つの除数しかない場合です。この場合、それは素数の完全な立方体です(その除数は立方根であり、立方根は二乗されています)。x**3==nこのケースをカバーし、キューブルートエントリに1を追加すると合計が3になり、誤検知が停止します。 この美しい説明を書いてくれたジョナサン・アランに感謝します


これは、8が半
素数である

n**(1/3)%1>0<sum...動作するはずです。
デニス

1
@xnorはそれを修正しました。
ロッド

小さな編集を行いました(たとえば、6キューブにはさらに多くの除数があります)
ジョナサンアラン

6

ルビー56 48バイト

->x{r=c=2;0while x%r<1?(x/=r;c-=1):x>=r+=1;c==0}

オンラインでお試しください!

使い方:

->x{                    # Lambda function
    r=c=2;              # Starting from r=2, c=2
    0 while             # Repeat (0 counts as a nop)
        x%r<1? (        # If x mod r == 0
            x/=r:       # Divide x by r
            c-=1        # decrease c
        ):              # else
            x>=r+=1     # increase r, terminate if r>x 
    );
    c==0                # True if we found 2 factors
}

8バイトを節約したというアイデアをValue Inkに感謝します。


cすべての要素を追加する配列にするのではなく、0から始めてカウントアップするだけではどうですか?そうすればsize、最後に使用する必要性を取り除くことができます
バリューインク

それは、別の課題のために因子分解関数を作成し、それをここで再利用したためです。
GB


4

これが4バイトである理由を説明できますか?...私は完全に混乱しています。
氏Xcoder

笑私は持っていたこの
HyperNeutrino

Mr.Xcoder NEIM @持つカスタムコードページ
JungHwan分

Mr.XcoderがNEIMコードページを使用して、@、これは𝐏𝐥δ、および𝔼のような単一バイト。
ハイパーニュートリノ

@HyperNeutrino私は2を少し難読化しましたが、これが2なしの唯一の答えです:)
Okx

4

Python 2、67バイト

lambda k:f(k)==2
f=lambda n,k=2:n/k and(f(n,k+1),1+f(n/k,k))[n%k<1]

オンラインでお試しください!

@JonathanAllanのおかげで-10バイト!

素因数分解アルゴリズムの功績はデニスに与えられます(初期バージョン)


デニスの答えのコードを使用しましたか?もしそうなら、あなたはクレジットを与える必要があります。
完全に人間

1
@totallyhumanそうそう、ごめんなさい。今日は2つの異なる回答で使用し、そのうちの1つでクレジットを与えましたが、ここでもう一度それを行うのを忘れました。見つけてくれてありがとう!
Mr Xcoder


@JonathanAllanうわー、ありがとう!
ミスターXcoder



4

Mathematica 32バイト

1バイトのngenesisのおかげで保存されました

Tr@FactorInteger[#][[;;,2]]==2&

1
;;代わりにを使用して1バイトを保存しますAll
-ngenisis






3

ガイア、4バイト

ḍl2=

4バイトは一般的な長さのようですが、なぜだろうか...:P

オンラインでお試しください!

説明

ḍ     Prime factors
 l    Length
  2=  Equals 2?

4バイトは一般的な長さのようですが、なぜだろうか ...-おそらくすべての答えは素因数であるため、長さは2に等しいのですか?
Mr Xcoder

@MrXcoderうん、まさに
ビジネス猫

4つは私のものですBTW> _>
Mr. Xcoder

4は最初のセミプライムでもあります。不気味な!
ニール




2

Java 8, 69 61 bytes

n->{int r=1,c=2;for(;r++<n;)for(;n%r<1;n/=r)c--;return c==0;}

-8 bytes thanks to @Nevay.

Try it here.


1
elseステートメント(可能性のあるelse++r;)を削除して、8バイトを節約できますn->{int r=1,c=2;for(;r++<n;)for(;n%r<1;n/=r)c--;return c==0;}
ネヴァイ


1

C#、112バイト

n=>{var r=Enumerable.Range(2,n);var l=r.Where(i=>r.All(x=>r.All(y=>y*x!=i)));return l.Any(x=>l.Any(y=>y*x==n));}

フォーマットが適用された場合:

n =>
{
    var r = Enumerable.Range (2, n);
    var l = r.Where (i => r.All (x => r.All (y => y * x != i)));
    return l.Any (x => l.Any (y => y * x == n));
}

そしてテストプログラムとして:

using System;
using System.Linq;


namespace S
{
    class P
    {
        static void Main ()
        {
            var f = new Func<int, bool> (
                n =>
                {
                    var r = Enumerable.Range (2, n);
                    var l = r.Where (i => r.All (x => r.All (y => y * x != i)));
                    return l.Any (x => l.Any (y => y * x == n));
                }
            );

            for (var i = 0; i < 100; i++)
                Console.WriteLine ($"{i} -> {f (i)}");
            Console.ReadLine ();
        }
    }
}

出力は次のとおりです。

0 -> False
1 -> False
2 -> False
3 -> False
4 -> True
5 -> False
6 -> True
7 -> False
8 -> False
9 -> True
10 -> True
11 -> False
12 -> False
13 -> False
14 -> True
15 -> True
16 -> False
17 -> False
18 -> False
19 -> False
20 -> False
21 -> True
22 -> True
23 -> False
24 -> False
25 -> True
26 -> True
27 -> False
28 -> False
29 -> False
30 -> False
31 -> False
32 -> False
33 -> True
34 -> True
35 -> True
36 -> False
37 -> False
38 -> True
39 -> True
40 -> False
41 -> False
42 -> False
43 -> False
44 -> False
45 -> False
46 -> True
47 -> False
48 -> False
49 -> True
50 -> False
51 -> True
52 -> False
53 -> False
54 -> False
55 -> True
56 -> False
57 -> True
58 -> True
59 -> False
60 -> False
61 -> False
62 -> True
63 -> False
64 -> False
65 -> True
66 -> False
67 -> False
68 -> False
69 -> True
70 -> False
71 -> False
72 -> False
73 -> False
74 -> True
75 -> False
76 -> False
77 -> True
78 -> False
79 -> False
80 -> False
81 -> False
82 -> True
83 -> False
84 -> False
85 -> True
86 -> True
87 -> True
88 -> False
89 -> False
90 -> False
91 -> True
92 -> False
93 -> True
94 -> True
95 -> True
96 -> False
97 -> False
98 -> False
99 -> False


1

Retina, 45 bytes

.+
$*
^(11+)(\1)+$
$1;1$#2$*
A`\b(11+)\1+\b
;

Try it online! Link includes test cases. Explanation:

.+
$*

Convert to unary.

^(11+)(\1)+$
$1;1$#2$*

Try to find two factors.

A`\b(11+)\1+\b

Ensure both factors are prime.

;

Ensure two factors were found.


1

Python 2, 90 bytes

def g(x,i=2):
 while x%i:i+=1
 return i
def f(n,l=0):
 while 1%n:l+=1;n/=g(n)
 return l==2

f takes an integer n greater than or equal to 1, returns boolean.

Try it online!

Test cases:

>>> f(1)
False
>>> f(2)
False
>>> f(3)
False
>>> f(4)
True
>>> f(6)
True
>>> f(8)
False
>>> f(30)
False
>>> f(49)
True
>>> f(95)
True

1

J, 6 bytes

5 bytes will work as a one-off:

   2=#q: 8
0
   2=#q: 9
1

I believe I need six when I define the function:

   semiprime =. 2=#@q:
   (,. semiprime) 1 + i. 20
 1 0
 2 0
 3 0
 4 1
 5 0
 6 1
 7 0
 8 0
 9 1
10 1
11 0
12 0
13 0
14 1
15 1
16 0
17 0
18 0
19 0
20 0


1

Japt, 6 5 bytes

k ʥ2

Test it online


Explanation

Does pretty much the same as most of the other answers: k gets the array of prime factors, Ê gets its length and ¥ checks for equality with 2.


÷k o)j also works, unfortunately it's the same length :-(
ETHproductions

0

Perl 6, 43 bytes

{my \f=first $_%%*,2..$_;?f&&is-prime $_/f}

Try it online!

f is the smallest factor greater than 1 of the input argument $_, or Nil if $_ is 1. The return value of the function is true if f is true (ie, not Nil) AND the input argument divided by the factor is prime.

If $_ itself is prime, then f will be equal to $_, and $_ / f is 1, which is not prime, so the formula works in that case as well.

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.