x.pl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # Date: 21 July 2018
  4. # https://github.com/trizen
  5. # Efficient algorithm for factoring an extended Chernick-Carmichael number.
  6. # First few extended Chernick-Carmichael numbers are:
  7. # 1729, 63973, 294409, 56052361
  8. # See also:
  9. # https://projecteuclid.org/euclid.bams/1183501763
  10. # https://oeis.org/wiki/Carmichael_numbers
  11. # https://en.wikipedia.org/wiki/Carmichael_number
  12. use 5.024;
  13. use warnings;
  14. use experimental qw(signatures);
  15. use List::Util qw(all);
  16. use ntheory qw(is_prob_prime);
  17. use Math::AnyNum qw(bsearch iroot ipow2 ilog2 prod);
  18. sub chernick_carmichael_factors ($k, $r) {
  19. (6 * $k + 1, 12 * $k + 1, (map { ipow2($_) * 9 * $k + 1 } 1 .. $r-2));
  20. }
  21. ##with the condition that each of the factors are prime and that m is divisible by 2^(k-4).
  22. #Extended Chernick Carmichael numbers.
  23. sub is_chernick_carmichael ($n) {
  24. foreach my $r (3 .. ilog2($n)) {
  25. my $t = iroot($n, $r);
  26. return 0 if (prod(chernick_carmichael_factors(1, $r)) > $n);
  27. my $k = bsearch(1, $t, sub {
  28. prod(chernick_carmichael_factors($_, $r)) <=> $n;
  29. });
  30. if (defined($k)) {
  31. if (all { is_prob_prime($_) } chernick_carmichael_factors($k, $r)) {
  32. return [$r, $k];
  33. }
  34. }
  35. }
  36. return 0;
  37. }
  38. # 1729, 63973, 294409, 56052361, 118901521, 172947529, 216821881, 228842209, 1299963601, 2301745249, 9624742921, 11346205609, 13079177569, 21515221081, 27278026129, 65700513721, 71171308081, 100264053529, 168003672409, 172018713961, 173032371289, 192739365541, 461574735553, 464052305161, 527519713969, 663805468801, 727993807201, 856666552249, 1042789205881, 1201586232601, 1396066334401, 1544001719761
  39. #1729, 63973, 294409, 56052361, 118901521, 172947529, 216821881, 228842209, 1299963601, 2301745249, 9624742921, 11346205609, 13079177569, 21515221081, 27278026129, 65700513721, 71171308081, 100264053529, 168003672409, 172018713961, 173032371289, 192739365541
  40. my $t = 1;
  41. while (defined(my $n = <>)) {
  42. (undef, $n) = split(' ', $n);
  43. $n // next;
  44. $n =~ /\S/ or do { say ''; next };
  45. $n = Math::AnyNum->new($n);
  46. if (my $indices = is_chernick_carmichael($n)) {
  47. #if (is_chernick_carmichael($n)) {
  48. my ($r, $k) = @$indices;
  49. say "M($r, $k) = $n";
  50. $k % 2**($r-4) == 0 or die "error";
  51. # say "$t $n";
  52. ++$t;
  53. #print "$n, ";
  54. }
  55. #else {
  56. # say "Not a Chernick-Carmichael number: $n";
  57. #}
  58. }
  59. __DATA__
  60. 8325544586081174440728309072452661246289
  61. 1486602098904402652768057938393756060862115780408050129
  62. 3378179316469672624194241840042044950902156938854178152235606615241
  63. 499363105138762800665090830700779256789861194424677603719907844311565991734904219234049
  64. 1052541934726120302251454117065809600311128515412938768050107822597914636735491079562159895572772335029969
  65. 179888061095822220624012979873
  66. 63295903488856146099776074891976628857941
  67. 1724903525088632276776203991973751571437217198753
  68. 125987992642689799129021757759222604492631818017403553
  69. 74630998863011672833530378836051056508973606035192155974373
  70. 150807169001103453136788769176330405141656863663445656308543366854744067292801145941
  71. 21481148526108486207494916467772828869885661326738699922267375224852562302202790454088898856458273
  72. 521635331852681575100906881
  73. 115062400756082746082903913434881
  74. 328163039680360319939589778453584981903661
  75. 11870677991315757722817424115344135399200189518509
  76. 694757711287970946444438020864958912321095838203403981194280844652135041
  77. 222047766292417414109702829403660230521393563058846142752440148661965564062512001
  78. 2149862240504463136613099818734059855038070454228745908492682225005023324481983560300180977379301646829
  79. 8708697287275863064616447198348134859079135616902774104816953554105827536430199092250104748403143942843541581649
  80. 837766669080429652091578576905732301415513036087717526534117797730213142822067681852966424142891732971385451048036269
  81. 261398323061911176816691559756701
  82. 3783580131711518790634677710442261470580569797344541
  83. 435371627429039040724001132527124473123288702163349741876813423106621
  84. 14719770617180585920139917829493719272506560558845969856660560241654606362030081
  85. 8639174282669715206025361687559030161351650277392264712967444363592650828493196768893181
  86. 5626560312723043583857755308221019825156276365042073078860543702210734827773374603314058575101
  87. 24556868549786120178074590558386520603888321
  88. 6039952244643618043250948311869286217356083814166356276064323543587107521
  89. 67237835600056002507521755422513656134639570320064261052894337496662546902793661
  90. 9812486963666228314195838164491424691687915196563926066688165613493816842244920774774301
  91. 16734371894003494165203863331927626808333173646940855138811711887045893525137741919908066470621