Primes: Difference between revisions

From NARS2000
Jump to navigationJump to search
No edit summary
 
(8 intermediate revisions by 2 users not shown)
Line 23: Line 23:
<p>For example,</p>
<p>For example,</p>


<apll>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;π120<br />
<apll><pre>
2 2 2 3 5<br />
      π120
<br />
2 2 2 3 5
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;×/⎕←π⎕←¯1+2*62<br />
 
4611686018427387903<br />
      ×/⎕←π⎕←¯1+2*62
3 715827883 2147483647<br />
4611686018427387903
4611686018427387903<br />
3 715827883 2147483647
<br />
4611686018427387903
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;π¯1+2*61<br />
 
2305843009213693951<br />
      π¯1+2*61
<br />
2305843009213693951
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⍴π1<br />
 
0</apll>
      ⍴π1
0
      c←×/(a b)←1π?2⍴10*25x  ⍝ Create two random 25-digit primes and their product
      a b c
213379889007584782100623 7822072437371562999056237 1669072948495632279507879309030439547039369735651
      πc  ⍝ Factor the 50-digit number
213379889007584782100623 7822072437371562999056237
      ⎕T-(πc)⊢⊢⎕T  ⍝ How many seconds of CPU time to factor a 50-digit number?
0.6204937084112316</pre></apll>




Line 48: Line 56:
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td>returns an integer scalar whose value depends upon which number-theoretic function is chosen by <apll>L</apll> to be applied to <apll>R</apll>.</td>
       <td>returns an array whose values depend upon which number-theoretic function chosen by <apll>L</apll> is applied to <apll>R</apll>.</td>
     </tr>
     </tr>
     </table>
     </table>
Line 57: Line 65:
     <table border="0" cellpadding="1" cellspacing="0" rules="none" summary="" style="margin-left: 20px;">
     <table border="0" cellpadding="1" cellspacing="0" rules="none" summary="" style="margin-left: 20px;">
       <tr>
       <tr>
         <td><apll>0</apll></td>
         <td><apll>¯2</apll></td>
         <td>&nbsp;&nbsp;Divisor count function</td>
         <td>&nbsp;&nbsp;Rth prime function</td>
       </tr>
       </tr>


       <tr>
       <tr>
         <td><apll>1</apll></td>
         <td><apll>¯1</apll></td>
         <td>&nbsp;&nbsp;Divisor sum function</td>
         <td>&nbsp;&nbsp;Previous prime function</td>
       </tr>
       </tr>


       <tr>
       <tr>
         <td><apll>2</apll></td>
         <td><apll>&nbsp;0</apll></td>
         <td>&nbsp;&nbsp;Primality test function</td>
         <td>&nbsp;&nbsp;Primality test function</td>
       </tr>
       </tr>


       <tr>
       <tr>
         <td><apll>3</apll></td>
         <td><apll>&nbsp;1</apll></td>
         <td>&nbsp;&nbsp;Next prime function</td>
         <td>&nbsp;&nbsp;Next prime function</td>
       </tr>
       </tr>


       <tr>
       <tr>
         <td><apll>4</apll></td>
         <td><apll>&nbsp;2</apll></td>
         <td>&nbsp;&nbsp;Previous prime function</td>
         <td>&nbsp;&nbsp;Number of primes function</td>
       </tr>
       </tr>


       <tr>
       <tr>
         <td><apll>5</apll></td>
         <td><apll>10</apll></td>
         <td>&nbsp;&nbsp;Nth prime function</td>
         <td>&nbsp;&nbsp;Divisor count function</td>
       </tr>
       </tr>


       <tr>
       <tr>
         <td><apll>6</apll></td>
         <td><apll>11</apll></td>
         <td>&nbsp;&nbsp;Number of primes function</td>
         <td>&nbsp;&nbsp;Divisor sum function</td>
       </tr>
       </tr>


       <tr>
       <tr>
         <td><apll>7</apll></td>
         <td><apll>12</apll></td>
         <td>&nbsp;&nbsp;Möbius function function</td>
         <td>&nbsp;&nbsp;Möbius function function</td>
       </tr>
       </tr>


       <tr>
       <tr>
         <td><apll>8</apll></td>
         <td><apll>13</apll></td>
         <td>&nbsp;&nbsp;Euler totient function</td>
         <td>&nbsp;&nbsp;Euler totient function</td>
       </tr>
       </tr>
Line 104: Line 112:
</tr>
</tr>
<tr>
<tr>
   <td><apll>R</apll> is a scalar consisting of a positive integer to which one of the above functions is applied.</td>
   <td><apll>R</apll> is an array consisting of positive integers to which one of the above functions is applied, element by element.</td>
</tr>
</tr>
<tr>
<tr>
   <td><apll>Z</apll> is an integer.</td>
   <td><apll>Z</apll> is an integer array of the same shape as <apll>R</apll>.</td>
</tr>
</tr>
</table>
</table>
<br />
<br />


== Divisor Count Function ==
== Rth Prime Function ==
 
<p>The Rth prime function (<apll>¯2πR</apll>) returns the <apll>R</apll><sup>th</sup> prime where <apll>2</apll> is the first prime.  This function is sensitive to the index origin.</p>
 
<p>For example, how many primes are less then or equal to <apll>1000003</apll>?</p>
 
<apll><pre>
      ¯2π1000003
15485927</pre></apll>
 
== Previous Prime Function ==


<p>The divisor count function (<apll>0πR</apll>) returns the number of divisors of a number.  It is the same as <apll>×/1+∪⍦πR</apll> where <apll>πR</apll> returns the prime factors of <apll>R</apll> and <apll>∪⍦</apll> counts the number of occurrences of unique elements (in this case, the exponent vector of the unique primes).  A divisor then consists of the product of zero or more of the unique primes which is why <apll>×/1+</apll> counts them.</p>
<p>The previous prime function (<apll>¯1πR</apll>) returns the prime that immediately precedes <apll>R</apll>.</p>


== Divisor Sum Function ==
<p>For example, what is the prime that immediately precedes <apll>1000000</apll>?</p>


<p>The divisor sum function (<apll>1πR</apll>) returns the sum of the divisors of a number.  It is the same as <apll>×/(¯1+(∪f)*1+∪⍦f)÷¯1+∪f←πR</apll><sup>1</sup>.  This function is used to recognize [http://en.wikipedia.org/wiki/Deficient_number deficient], [http://en.wikipedia.org/wiki/Perfect_number perfect], and [http://en.wikipedia.org/wiki/Abundant_number abundant] numbers.</p>
<apll><pre>
      ¯1π1000000
999983</pre></apll>


== Primality Test ==
== Primality Test ==


<p>The primality test function (<apll>2πR</apll>) returns a <apll>1</apll> if <apll>R</apll> is a prime and <apll>0</apll> if not.</p>
<p>The primality test function (<apll>0πR</apll>) returns a <apll>1</apll> if <apll>R</apll> is a prime and <apll>0</apll> if not.</p>
 
<p>For example, is <apll>1000003</apll> a prime?</p>
 
<apll><pre>
      0π1000003
1</pre></apll>
 
<p>List the primes up to 100</p>
 
<apll><pre>
      ⍸0π⍳100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</pre></apll>


== Next Prime Function ==
== Next Prime Function ==


<p>The next prime function (<apll>3πR</apll>) returns the prime that immediately follows <apll>R</apll>.</p>
<p>The next prime function (<apll>1πR</apll>) returns the prime that immediately follows <apll>R</apll>.</p>
 
<p>For example, what is the next prime after <apll>1000000</apll>?</p>
 
<apll><pre>
      1π1000000
1000003</pre></apll>
 
== Number Of Primes Function ==
 
<p>The number of primes function (<apll>2πR</apll>) returns number of primes less than or equal to <apll>R</apll>.</p>


== Previous Prime Function ==
<p>For example, what is the <apll>15485927</apll>th prime?</p>


<p>The previous prime function (<apll>4πR</apll>) returns the prime that immediately precedes <apll>R</apll>.</p>
<apll><pre>
      2π15485927
1000003</pre></apll>


== Nth Prime Function ==
== Divisor Count Function ==


<p>The Nth prime function (<apll>5πR</apll>) returns the <apll>R</apll><sup>th</sup> prime where <apll>2</apll> is the first prime.</p>
<p>The divisor count function (<apll>10πR</apll>) returns the number of divisors of a number.  It is the same as <apll>×/1+∪⍦πR</apll> where <apll>πR</apll> returns the prime factors of <apll>R</apll> and <apll>∪⍦</apll> counts the number of occurrences of unique elements (in this case, the exponent vector of the unique primes).  A divisor then consists of the product of zero or more of the unique primes which is why <apll>×/1+</apll> counts them.</p>


== Number Of Primes Function ==
== Divisor Sum Function ==


<p>The number of primes function (<apll>6πR</apll>) returns number of primes less than or equal to <apll>R</apll>.</p>
<p>The divisor sum function (<apll>11πR</apll>) returns the sum of the divisors of a number.  It is the same as <apll>×/(¯1+(∪f)*1+∪⍦f)÷¯1+∪f←πR</apll><sup>1</sup>.  This function is used to recognize [http://en.wikipedia.org/wiki/Deficient_number deficient], [http://en.wikipedia.org/wiki/Perfect_number perfect], and [http://en.wikipedia.org/wiki/Abundant_number abundant] numbers.</p>


== Möbius Function ==
== Möbius Function ==


<p>The [http://en.wikipedia.org/wiki/M%C3%B6bius_function Möbius function] (<apll>7πR</apll>) returns information about the square free properties of <apll>R</apll>.  If <apll>R</apll> is [http://en.wikipedia.org/wiki/Square-free_integer square free], the function returns <apll>1</apll> if <apll>R</apll> has an even number of prime factors, and <apll>¯1</apll> if it has an odd number of prime factors.  If the argument is not square free, the function returns <apll>0</apll>.  It is used in the [http://en.wikipedia.org/wiki/M%C3%B6bius_inversion_formula Möbius Inversion Formula] to invert general arithmetic functions.</p>
<p>The [http://en.wikipedia.org/wiki/M%C3%B6bius_function Möbius function] (<apll>12πR</apll>) returns information about the square free properties of <apll>R</apll>.  If <apll>R</apll> is [http://en.wikipedia.org/wiki/Square-free_integer square free], the function returns <apll>1</apll> if <apll>R</apll> has an even number of prime factors, and <apll>¯1</apll> if it has an odd number of prime factors.  If the argument is not square free, the function returns <apll>0</apll>.  It is used in the [http://en.wikipedia.org/wiki/M%C3%B6bius_inversion_formula Möbius Inversion Formula] to invert general arithmetic functions.</p>


== Totient Function ==
== Totient Function ==


<p>The totient function (<apll>8πR</apll>) (also called [http://en.wikipedia.org/wiki/Euler_phi Euler's Totient Function]) returns the number of positive integers less than or equal to <apll>R</apll> that are relatively prime to it (i.e., having no common positive factors other than 1).
<p>The totient function (<apll>13πR</apll>) (also called [http://en.wikipedia.org/wiki/Euler_phi Euler's Totient Function]) returns the number of positive integers less than or equal to <apll>R</apll> that are relatively prime to it (i.e., having no common positive factors other than 1).
</p>
</p>
== Examples ==
<p>Add together the first 100 primes:</p>
<p>The Rth prime function (<apll>¯2πR</apll>) gives the value of the Rth prime, as in</p>
<apll><pre>
      ¯2π100
541</pre></apll>
<p>The Index function (<apll>⍳R</apll>) produces a vector of integers of length <apll>R</apll>, as in</p>
<apll><pre>
      ⍳¯2π100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ... 541</pre></apll>
<p>The Primality Test function (<apll>0πR</apll>) returns a <apll>1</apll> if the corresponding element in <apll>R</apll> is a prime, <apll>0</apll> otherwise, as in</p>
<apll><pre>
      0π⍳¯2π100
0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 ... 1</pre></apll>
<p>The Indices function (<apll>⍸R</apll>) converts the argument <apll>R</apll> to indices (equivalent to <apll>(,R)/⍳×/⍴R</apll>), as in</p>
<apll><pre>
      ⍸0π⍳¯2π100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 ... 541</pre></apll>
<p>Finally, those numbers may be added together using plus reduction (<apll>+/</apll>), as in</p>
<apll><pre>
      +/⍸0π⍳¯2π100
24133</pre></apll>


== Notes ==
== Notes ==


<sup>1</sup> [http://planetmath.org/encyclopedia/FormulaForSumOfDivisors.html Formula for sum of divisors]
<sup>1</sup> [https://en.wikipedia.org/wiki/Divisor_function Formula for sum of divisors]

Latest revision as of 20:59, 17 July 2020

Z←πR returns an integer vector consisting of the prime factors of R.
R is a scalar or one-element vector consisting of a positive integer to be factored.
Z is an integer vector whose values are the prime factors of R.


For example,

      π120
2 2 2 3 5

      ×/⎕←π⎕←¯1+2*62
4611686018427387903
3 715827883 2147483647
4611686018427387903

      π¯1+2*61
2305843009213693951

      ⍴π1
0
      c←×/(a b)←1π?2⍴10*25x  ⍝ Create two random 25-digit primes and their product
      a b c
213379889007584782100623 7822072437371562999056237 1669072948495632279507879309030439547039369735651 
      πc  ⍝ Factor the 50-digit number
213379889007584782100623 7822072437371562999056237 
      ⎕T-(πc)⊢⊢⎕T  ⍝ How many seconds of CPU time to factor a 50-digit number?
0.6204937084112316



Z←LπR returns an array whose values depend upon which number-theoretic function chosen by L is applied to R.
L is an integer scalar whose meaning is as follows
¯2   Rth prime function
¯1   Previous prime function
 0   Primality test function
 1   Next prime function
 2   Number of primes function
10   Divisor count function
11   Divisor sum function
12   Möbius function function
13   Euler totient function
R is an array consisting of positive integers to which one of the above functions is applied, element by element.
Z is an integer array of the same shape as R.


Rth Prime Function

The Rth prime function (¯2πR) returns the Rth prime where 2 is the first prime. This function is sensitive to the index origin.

For example, how many primes are less then or equal to 1000003?

      ¯2π1000003
15485927

Previous Prime Function

The previous prime function (¯1πR) returns the prime that immediately precedes R.

For example, what is the prime that immediately precedes 1000000?

      ¯1π1000000
999983

Primality Test

The primality test function (0πR) returns a 1 if R is a prime and 0 if not.

For example, is 1000003 a prime?

      0π1000003
1

List the primes up to 100

      ⍸0π⍳100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Next Prime Function

The next prime function (1πR) returns the prime that immediately follows R.

For example, what is the next prime after 1000000?

      1π1000000
1000003

Number Of Primes Function

The number of primes function (2πR) returns number of primes less than or equal to R.

For example, what is the 15485927th prime?

      2π15485927
1000003

Divisor Count Function

The divisor count function (10πR) returns the number of divisors of a number. It is the same as ×/1+∪⍦πR where πR returns the prime factors of R and ∪⍦ counts the number of occurrences of unique elements (in this case, the exponent vector of the unique primes). A divisor then consists of the product of zero or more of the unique primes which is why ×/1+ counts them.

Divisor Sum Function

The divisor sum function (11πR) returns the sum of the divisors of a number. It is the same as ×/(¯1+(∪f)*1+∪⍦f)÷¯1+∪f←πR1. This function is used to recognize deficient, perfect, and abundant numbers.

Möbius Function

The Möbius function (12πR) returns information about the square free properties of R. If R is square free, the function returns 1 if R has an even number of prime factors, and ¯1 if it has an odd number of prime factors. If the argument is not square free, the function returns 0. It is used in the Möbius Inversion Formula to invert general arithmetic functions.

Totient Function

The totient function (13πR) (also called Euler's Totient Function) returns the number of positive integers less than or equal to R that are relatively prime to it (i.e., having no common positive factors other than 1).

Examples

Add together the first 100 primes:

The Rth prime function (¯2πR) gives the value of the Rth prime, as in

      ¯2π100
541

The Index function (⍳R) produces a vector of integers of length R, as in

      ⍳¯2π100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ... 541

The Primality Test function (0πR) returns a 1 if the corresponding element in R is a prime, 0 otherwise, as in

      0π⍳¯2π100
0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 ... 1

The Indices function (⍸R) converts the argument R to indices (equivalent to (,R)/⍳×/⍴R), as in

      ⍸0π⍳¯2π100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 ... 541

Finally, those numbers may be added together using plus reduction (+/), as in

      +/⍸0π⍳¯2π100
24133

Notes

1 Formula for sum of divisors