Pan base non-primes

From Rosetta Code
Revision as of 01:11, 31 July 2022 by Thundergnat (talk | contribs) (→‎{{header|Raku}}: Add correct header)
Pan base non-primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Primes are prime no matter which base they are expressed in. Some numeric strings are prime in a large number of bases. (Not the same prime, but a prime.)

For example

The numeric string "255", while obviously not a prime in base 10, is a prime in bases:

     6   8  12  14  18  24  26  32  36  38  72  84  86  92  96 102 104 108 128 134
   138 144 158 164 188 216 224 236 242 246 252 254 264 272 294 318 332 344 348 368
   374 392 396 408 428 432 446 456 468 476 482 512 522 528 542 546 552 566 572 576
   578 594 596 602 606 614 618 626 654 702 714 722 728 756 762 774 776 788 806 816
   818 822 828 836 848 864 866 872 882 888 902 908 912 918 924 932 936 942 944 956
   966 986 998

among others.

There are numeric strings however, that are not a prime in any base. Confining ourselves to 'decimal' numeric strings; the single digit numeric primes are prime in every base where they are a valid number.


E.G.

The numeric string "2" is a prime in every base except base 2, where it is invalid.

The numeric string "3" is a prime in every base except base 2 and 3, where it is invalid.

"4" is not a prime in every base except bases 2, 3, and 4 where it is an invalid number (and hence not a prime there either.)


In general, even pan-base non-primes are much more prevalent than odd, though both are fairly common.

With the exception of "10", numeric strings that end in 0 are composite in every base where they are valid.

Numeric strings where the greatest common divisor of all of the digits is more than 1 are composite in every base.

If a "decimal" numeric string N is composite in every base up to base N, it is composite in every base.


Task
  • Find and display, here on this page, the first 40 pan-base non-prime "base 10" numeric strings.
  • Find and display, here on this page, the first 20 odd pan-base non-prime "base 10" numeric strings.
  • Find and display the count of pan-base non-prime "base 10" numeric strings up to at least the numeric string "1000".
  • What percentage of them are odd / even?


See also

J

Implementation:<lang J>pbnp=: {{ if. 10 > y do. -.1 p: y return. end.

 digits=. 10 #.inv y
 */0=1 p: ((>./digits)+i.y) #."0 1 digits

}}"0</lang> Task examples:<lang J> 40{.1+I.pbnp 1+i.1e3 NB. first 40 pan based non primes 1 4 6 8 9 20 22 24 26 28 30 33 36 39 40 42 44 46 48 50 55 60 62 63 64 66 68 69 70 77 80 82 84 86 88 90 93 96 99 100

  20{.(#~ 2&|)1+I.pbnp 1+i.1e3 NB. first 20 odd pan based non primes

1 9 33 39 55 63 69 77 93 99 121 143 165 169 187 231 253 273 275 297

  #(#~ 2&|)1+I.pbnp 1+i.1e3 NB. number of pan based non primes up to 1000

64

  100*(+/%#)2|1+I.pbnp 1+i.1e3 NB. percent odd pan based non primes up to 1000

16.9761</lang>

Raku

<lang perl6>use Base::Any; use List::Divvy;

my @np = 4,6,8,9, |lazy (11..*).hyper.grep( -> $n { ($n.substr(*-1) eq '0') || (1 < [gcd] $n.comb».Int) || none (2..$n).map: { try "$n".&from-base($_).is-prime } } );

put "First 50 pan-base non-primes:\n" ~ @np[^50].batch(10)».fmt("%3s").join: "\n"; put "\nFirst 20 odd pan-base non-primes:\n" ~ @np.grep(* % 2)[^20].batch(10)».fmt("%3s").join: "\n";

my $threshold = 2500; put "\nCount of pan-base non-primes up to and including $threshold: " ~ +@np.&upto($threshold);

put "Percent odd up to and including $threshold: " ~ +@np.&upto($threshold).grep(* % 2) / +@np.&upto($threshold) × 100; put "Percent even up to and including $threshold: " ~ +@np.&upto($threshold).grep(* %% 2) / +@np.&upto($threshold) × 100;</lang>

Output:
First 50 pan-base composites:
  4   6   8   9  20  22  24  26  28  30
 33  36  39  40  42  44  46  48  50  55
 60  62  63  64  66  68  69  70  77  80
 82  84  86  88  90  93  96  99 100 110
112 114 116 118 120 121 130 132 134 136

First 20 odd pan-base composites:
  9  33  39  55  63  69  77  93  99 121
143 165 169 187 231 253 273 275 297 299

Count of pan-base composites up to and including 2500: 953
Percent odd  up to and including 2500: 16.894019
Percent even up to and including 2500: 83.105981