Erdös-Selfridge categorization of primes: Difference between revisions

Functional implementation for Mathematica / Wolfram language
m (→‎{{header|Wren}}: Changed to Wren S/H)
(Functional implementation for Mathematica / Wolfram language)
Line 579:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
===Imperative implementation===
<syntaxhighlight lang="mathematica">ClearAll[SpecialPrimeFactors]
SpecialPrimeFactors[p_Integer] := FactorInteger[p + 1][[All, 1]]
Line 675 ⟶ 676:
10 1065601 15472321 28
11 8524807 8524807 1</pre>
 
===Functional implementation===
<syntaxhighlight lang="mathematica">ClearAll[ErdosSelfridgeCategory, CategorySizesTable, categories];
 
ErdosSelfridgeCategory[n_] := ErdosSelfridgeCategory[n] =
If[ Last[primesToCheck = FactorInteger[n + 1][[All, 1]]] <= 3,
1,
1 + Max[ErdosSelfridgeCategory /@ primesToCheck]
];
 
(* Display the first 200 primes allocated to their category *)
 
categories = GatherBy[Prime[Range[200]], ErdosSelfridgeCategory];
 
Print["First 200 primes by Erdös-Selfridge category:\n"]
 
Do[
Print["Class ", i, ":"];
Print[
TableForm[
Partition[
Map[StringPadLeft[ToString[#], 5] &, categories[[i]]], UpTo[12]],
TableAlignments -> Right]
],
{i, Length[categories]}
];
 
(* Show the number of primes in each category for the first million primes *)
 
Print["Calculating Erdős-Selfridge categories for the first million primes...."]
 
{seconds, categories} =
AbsoluteTiming[GatherBy[Prime[Range[10^6]], ErdosSelfridgeCategory]];
 
Print["Calculation took ", seconds " seconds."];
 
CategorySizesTable[categories_List] :=
TableForm[
Table[{Min[categories[[n]]], Max[categories[[n]]],
Length[categories[[n]]]}, {n, Length[categories]}],
TableHeadings -> {Automatic, {"First", "Last", "Count"}},
TableAlignments -> Right];
 
Print[CategorySizesTable[categories]]</syntaxhighlight>
 
{{out}}
<pre>
First 200 primes by Erdös-Selfridge category:
 
Class 1:
2 3 5 7 11 17 23 31 47 53 71 107
127 191 383 431 647 863 971 1151
 
Class 2:
13 19 29 41 43 59 61 67 79 83 89 97
101 109 131 137 139 149 167 179 197 199 211 223
229 239 241 251 263 269 271 281 283 293 307 317
349 359 367 373 419 433 439 449 461 479 499 503
509 557 563 577 587 593 599 619 641 643 659 709
719 743 751 761 769 809 827 839 881 919 929 953
967 991 1019 1033 1049 1069 1087 1103 1187 1223
 
Class 3:
37 103 113 151 157 163 173 181 193 227 233 257
277 311 331 337 347 353 379 389 397 401 409 421
457 463 467 487 491 521 523 541 547 569 571 601
607 613 631 653 683 701 727 733 773 787 797 811
821 829 853 857 859 877 883 911 937 947 983 997
1009 1013 1031 1039 1051 1061 1063 1091 1097 1117 1123 1153
1163 1171 1181 1193 1217
 
Class 4:
73 313 443 617 661 673 677 691 739 757 823 887
907 941 977 1093 1109 1129 1201 1213
 
Class 5:
1021
 
Calculating Erdős-Selfridge categories for the first million primes....
Calculation took 10.2376 seconds.
 
First Last Count
1 2 10616831 46
2 13 15482669 10497
3 37 15485863 201987
4 73 15485849 413891
5 1021 15485837 263109
6 2917 15485857 87560
7 15013 15484631 19389
8 49681 15485621 3129
9 532801 15472811 363
10 1065601 15472321 28
11 8524807 8524807 1
</pre>
 
=={{header|Nim}}==
6

edits