Primzahlsiebe / Filtres pour nombres premiers

(Mit Vorschlägen von Studenten / Ceretains solutions ont été proposées par des étudiants)

Primzahlen / Nombres premiers

f[x_Integer /; PrimeQ[x] == True] := x;

f[x_] := 1; f[x_Integer /; PrimeQ[x] == True] := x;  
    prim[n_] := Union[Table[f[x], {x, 1, n}]]; prim[10000]

?Prime

Prime[Range[100]]

Prime[Range[(10^9) + 1, (10^9) + 20]]

Mit Do / Avec Do

Zwillinge

zwi1[n_] := 
      Do[If[p1 - p2 == 2 &&  PrimeQ[p1]  &&   PrimeQ[p2], 
          Print[p1,  "=",  p2, "+", "2"]], {p1, 2, n}, {p2, 2, 
          n}]

zwi1[1000]

p1 = p2 + p3 + 1

driAdd1[n_] := 
      Do[If[PrimeQ[p2 + p3 + 1] &&  
            PrimeQ[p2]  &&   PrimeQ[p3]  &&   p2 > p3, 
          Print[p2 + p3 + 1,  "=",  p2, "+", 
            p3, "+", "1"]], {p2, 2, n}, {p3, 2, n}]

driAdd1[30]

? =!=

?Not

?!=

Remove[driAdd2]

driAdd2[n_] := 
      Do[If[PrimeQ[p1] &&  
            PrimeQ[p2]  &&   PrimeQ[p3]  &&   p2 > p3  &&  
            p1 == p2 + p3 + 1, 
          Print[p1,  "=",  p2, "+", p3, "+", "1"]], {p1, 2, 
          n}, {p2, 2, n}, {p3, 2, n}]

driAdd2[30]

p1 = p2 + p3 - 1

driAdd3[n_] := 
      Do[If[PrimeQ[p2 + p3 - 1] &&  
            PrimeQ[p2]  &&   PrimeQ[p3]  &&   p2 > p3, 
          Print[p2 + p3 + 1,  "=",  p2, "+", 
            p3, "-", "1"]], {p2, 2, n}, {p3, 2, n}]

driAdd3[30]

p1 = p2 * p3 + 2

driMult1[n_] := 
      Do[If[PrimeQ[p2*p3 + 2] &&  
            PrimeQ[p2]  &&   PrimeQ[p3]  &&   p2 > p3, 
          Print[p2*p3 + 2,  "=",  p2, "*", 
            p3, "+", "2"]], {p2, 2, n}, {p3, 2, n}]

driMult1[30]

p1 = p2 * p3 - 2

driMult2[n_] := 
      Do[If[PrimeQ[p2*p3 - 2] &&  
            PrimeQ[p2]  &&   PrimeQ[p3]  &&   p2 > p3, 
          Print[p2*p3 + 2,  "=",  p2, "*", 
            p3, "-", "2"]], {p2, 2, n}, {p3, 2, n}]

driMult2[30]

&&  p2 > p3,

p1 = p2 ^ p3+ 2

driPot1[n_] := 
      Do[If[PrimeQ[p2^p3 + 2] &&  PrimeQ[p2]  &&   PrimeQ[p3] , 
          Print[p2^p3 + 2,  "=",  p2, "^", 
            p3, "+", "2"]], {p2, 2, n}, {p3, 2, n}]

driPot1[20]

p1 = p2 ^ p3- 2

driPot2[n_] := 
      Do[If[PrimeQ[p2^p3 - 2] &&  PrimeQ[p2]  &&   PrimeQ[p3] , 
          Print[p2^p3 + 2,  "=",  p2, "^", 
            p3, "-", "2"]], {p2, 2, n}, {p3, 2, n}]

driPot2[20]

p1 +p2 - p3- 3

driAdd4[n_] := 
      Do[If[PrimeQ[p1] &&  
            PrimeQ[p2]  &&   PrimeQ[p3]  &&   p1 > p2  &&  
            p2  ≠ p3  && 
            p1  ≠ p3  &&   p1 + p2 - p3 == 3, 
          Print[p1,  "+",  p2, "-", p3, "=", "3"]], {p1, 2, 
          n}, {p2, 2, n}, {p3, 2, n}]

driAdd4[50]

Do Do Do

dododo[n_] := 
      Do[Do[Do[If[Prime[x] == Prime[y]*Prime[z] + 2, 
              Print[Prime[x], " = ", Prime[y], " * ", 
                Prime[z], " + 2"]], {x, 2, n}, {y, 2, n}, {z, 2, 
              n}]]]

dododo[20]

dododo1[n_] := 
      Do[Do[Do[If[
              Prime[x] == (Floor[Prime[x]/2] + Prime[y])/Prime[z], 
              Print[Prime[x], " = ([", Prime[x], " /2] + ", 
                Prime[y], ")/", Prime[z]]], {x, 2, n}, {y, 2, n}, {z, 2, 
              n}]]]

dododo1[60]

Mit While und NextPrime / Avec While et NextPrime

<< NumberTheory`NumberTheoryFunctions`

NextPrime[10^64]

tabZwi[n_] := {i = 0; k = 0; nP = 0; 
        While[k < n, 
          If[PrimeQ[i], np = NextPrime[i];  
            If[np - i == 2, 
              Print[i,  " und " ,  
                np]; k++];]; i++]}

tabZwi[30]

nextPrim[n_Integer] := 
      Module[{k = n}, While[! PrimeQ[k], k++]; Return[k]]

nextPrim[10]

nextPrim[25]

primZwiNew1[n_Integer] := 
      For[k = 0, k < n, k++, 
        If[PrimeQ[k], 
          If[nextPrim[k + 1] - k == 2, 
            Print[nextPrim[k + 1], " = ", k, " + 2"]]]]

primZwiNew1[1000]

primSumNew1[n_Integer] := 
      For[k = 0, k < n, k++, 
        If[PrimeQ[k], 
          If[PrimeQ[nextPrim[k + 1] + k + 1], 
            Print[nextPrim[k + 1] + k + 1, " = ", 
              nextPrim[k + 1], " + ", k, " + 1"]]]]

primSumNew1[100]

primProdNew1[n_Integer] := 
      For[k = 0, k < n, k++, 
        If[PrimeQ[k], 
          If[PrimeQ[nextPrim[k + 1]*k + 2], 
            Print[nextPrim[k + 1]*k + 2, " = ", 
              nextPrim[k + 1], " * ", k, " + 2"]]]]

primProdNew1[100]

primPotNew1[n_Integer] := 
      For[k = 0, k < n, k++, 
        If[PrimeQ[k], 
          If[PrimeQ[2^k - 1], 
            Print[2^k - 1, " = ", 2, " ^ ( ", 
              k, " -1)"]]]]

primPotNew1[100]

primPotNew2[n_Integer] := 
      For[k = 0, k < n, k++, 
        If[PrimeQ[k], 
          If[PrimeQ[2^k + 1], 
            Print[2^k + 1, " = ", 2, " ^ ( ", 
              k, " +1)"]]]]

primPotNew2[1000]

Mit For und If / Avec For et If

primAdd[a_, b_] := 
      Module[{x = a, 
          y = b}, {For[p2 = 0, p2 ≤  x, 
            For[p3 = 0, p3 ≤  y, 
              If[PrimeQ[p2] == True, 
                If[PrimeQ[p3] == True, 
                  If[PrimeQ[p2 + p3 + 1] == True, 
                    Print[p2 + p3 + 1, "= ( ", p2, "+", 
                      p3, "+1 )"]]]]; p3++]; p2++]}]

primAdd[29, 19]

primAddGeordnet[a_, b_, c_] := 
      Module[{x = a, y = b, 
          z = c}, {For[p2 = 0, p2 ≤  x, 
            For[p3 = 0, p3 ≤  y, 
              If[PrimeQ[p2] == True, 
                If[PrimeQ[p3] == True, 
                  If[PrimeQ[p2 + p3 + 1] == True, 
                    If[p2 + p3 + 1 ≤ z, 
                      Print[p2 + p3 + 1, "= ( ", p2, "+", 
                        p3, "+1 )"]]]]]; p3++]; p2++]}]

primAddGeordnet[29, 19, 30]

primMult[a_] := 
      Module[{x = a}, {For[p2 = 0, p2 ≤  x, 
            For[p3 = 0, p3 ≤  x, 
              If[PrimeQ[p2] == True, 
                If[PrimeQ[p3] == True, 
                  If[PrimeQ[p2*p3 + 2] == True, 
                    Print[p2*p3 + 2, "= ( ", p2, "*", 
                      p3, "+2 )"]]]]; p3++]; p2++]}]

primMult[29]

primMultAdd[a_, n_] := 
      Module[{x = a}, {For[p2 = 0, p2 ≤  x, 
            For[p3 = 0, p3 ≤  x, 
              If[PrimeQ[p2] == True, 
                If[PrimeQ[p3] == True, 
                  If[PrimeQ[p2 + n*p3] == True, 
                    Print[p2 + n*p3, "= ( ", p2, "+", n, "*", 
                      p3, " )"]]]]; p3++]; p2++]}]

primMultAdd[29, 2]

primMultAdd[29, 10]

Mit anonymen Variablen / Avec variables anonymes

Herleitung / Trouver une méthode

r1 = Prime[Range[10]]

r2 = Drop[r1, 1]

r3 = Drop[r1, 4]

r4 = Drop[r1, -1]

r5 = Drop[r1, -5]

r2 - r4

r1 - r1

Position[r2 - r4, 1]

Position[r2 - r4, 2]

Position[r2 - r4, 3]

Position[r2 - r4, 6]

?Position

?Extract

(mm = Array[10  #1 + #2 &, {3, 3}]) // MatrixForm

Extract[mm, {2, 3}]

Extract[r1, Position[r2 - r4, 2]]

?#

\(\(#1\^#2 &\)[2, 1]\)

\(\(#1\^#2 &\)[2, 2]\)

\(\(#1\^#2 &\)[2, 3]\)

\(\(#1\^#2 &\)[3, 2]\)

?/@

Map[f, {{a, b}, {c, d}}, {2}]

f /@ {{a, b}, {c, d}}

primZwil[n_] := 
      Module[{r = Prime[Range[n]]}, {#, # + 2} & /@ 
          Extract[r, Position[Drop[r, 1] - Drop[r, -1], 2]]]

primZwil[10]

primZwil[100]

primDistVier[n_] := 
      Module[{r = Prime[Range[n]]}, {#, # + 4} & /@ 
          Extract[r, Position[Drop[r, 1] - Drop[r, -1], 4]]]

primDistVier[100]

primDistm[n_, m_] := 
      Module[{r = Prime[Range[n]]}, {#, # + m} & /@ 
          Extract[r, Position[Drop[r, 1] - Drop[r, -1], m]]]

primDistm[1000, 10]