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.

Wolfram Language で素数で遊ぶ

Last updated at Posted at 2020-02-08

N 以下の素数を求める

In[2]:= Prime @ Range @ PrimePi[50]
Out[2]= {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}

小さな値から順に N 個の素数を求める

In[7]:= Prime @ Range[10]
Out[7]= {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}

N 番目の素数を求める

In[4]:= Prime[1]
Out[4]= 2

In[5]:= Prime[2]
Out[5]= 3

In[6]:= Prime[100]
Out[6]= 541

N を素因数分解する

In[8]:= FactorInteger[2600]
Out[8]= {{2, 3}, {5, 2}, {13, 1}}
In[20]:= Join @@ (Table[#[[1]], #[[2]]] & /@ FactorInteger[2600])
Out[20]= {2, 2, 2, 5, 5, 13}

N が素数かどうか調べる

In[21]:= PrimeQ[13]
Out[21]= True

In[22]:= PrimeQ[1024]
Out[22]= False

N 以上の最小の素数を求める

In[23]:= f[n_] := NextPrime[n-1]

In[24]:= f[13]
Out[24]= 13

In[25]:= f[14]
Out[25]= 17

N 以下の最大の素数を求める

In[39]:= g[n_] := NextPrime[n+1, -1]

In[40]:= g[3]
Out[40]= 3

In[41]:= g[100]
Out[41]= 97

N 以上の素数を、小さな順から M 個求める

In[48]:= h[n_, m_] := NestList[NextPrime, NextPrime[n-1], m-1]

In[49]:= h[1, 3]
Out[49]= {2, 3, 5}

In[50]:= h[3, 5]
Out[50]= {3, 5, 7, 11, 13}

約数の個数を求める

In[52]:= DivisorSigma[0, 2^2 * 5]
Out[52]= 6

In[53]:= DivisorSigma[0, 1024]
Out[53]= 11

約数の総和を求める

In[54]:= DivisorSigma[1, 12]
Out[54]= 28

In[55]:= DivisorSigma[1, 1800]
Out[55]= 6045

約数を列挙する

In[56]:= Divisors[10]
Out[56]= {1, 2, 5, 10}

In[57]:= Divisors[1024]
Out[57]= {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}

リンク

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?