Penholodigital squares

Revision as of 07:20, 4 February 2023 by Rdm (talk | contribs) (→‎{{header|J}}: meh)

Penholodigital squares are perfect square numbers that contain all of the digits from the base in which the number is represented, except for zero, exactly once.

Penholodigital squares 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.
From the Latin prefix pene- (before, or next to, nearly)

and holo- (whole, or all)

penholodigital: Nearly-all-digits.

So, in a particular base, a penholodigital square number will contain all of the digits used in that base (except zero) once, and only once. Base eight penholodigitals contain the digits 1 through 7, base 10, 1 through 9, etc.


For example

In base 10, 139854276 is a penholodigital square. It is the square of the integer 11826, and contains every digit from 1 through 9 exactly once.


Penholodigital squares can occur in many, though not every, base. They tend to be pretty rare in lower bases.

There is a total of 1 penholodigital squares in base 2:
1² = 1

There is a total of 0 penholodigital squares in base 3:

There is a total of 0 penholodigital squares in base 4:

There is a total of 0 penholodigital squares in base 5:

There is a total of 2 penholodigital squares in base 6:
122² = 15324, 221² = 53241

There is a total of 1 penholodigital squares in base 7:
645² = 623514

There is a total of 1 penholodigital squares in base 8:
2453² = 6532471


Task

Find and display the total count, and the penholodigital squares and the integers that are squared to produce them, represented in the base in which they are calculated, for bases 9, 10, 11 and 12.


Stretch

Find and display the total count, and the first and last penholodigital squares and the integers that are squared to produce them, represented in the base in which they are calculated, for bases 13, 14, 15, ... ?


See also


J

Implementation:

digch=: a.{~;48 97(+i.)&.>10 26
brep=: (digch {~ #.inv)&.>

penholod=: {{
  F=: >.%:y#.D=:}.i.y
  C=: <.%:y#.}:i.-y
  ok=: (D */@e. y #.inv ])"0
  (#~ok) *:F+i.1+C-F
}}

task=: {{
  sq=. penholod y
  hd=. ,:(#sq),&":' penholodigital squares in base ',":y
  hd,(*#sq)#names (y brep sq),each '=',each(y brep %:sq),each<'²'
}}

stretch=: {{
  sq=. penholod y
  hd=. ,:(#sq),&":' penholodigital squares in base ',":y
  hd,(*#sq)#names ({.,'...';{:) (y brep sq),each '=',each(y brep %:sq),each<'²'
}}

Task examples:

   task 9
10 penholodigital squares in base 9                                                             
16328547=3825² 16523874=3847² 23875614=4617² 25487631=4761² 47865231=6561² 48162537=6574² 
53184267=6844² 58624317=7285² 68573241=7821² 82314657=8554²                                 
   task 10
30 penholodigital squares in base 10                                                                        
139854276=11826² 152843769=12363² 157326849=12543² 215384976=14676² 245893761=15681² 254817369=15963² 
326597184=18072² 361874529=19023² 375468129=19377² 382945761=19569² 385297641=19629² 412739856=20316² 
523814769=22887² 529874361=23019² 537219684=23178² 549386721=23439² 587432169=24237² 589324176=24276² 
597362481=24441² 615387249=24807² 627953481=25059² 653927184=25572² 672935481=25941² 697435281=26409² 
714653289=26733² 735982641=27129² 743816529=27273² 842973156=29034² 847159236=29106² 923187456=30384² 
   task 11
20 penholodigital squares in base 11                                                           
165742a893=42045² 173a652894=43152² 18792a6453=44926² 1a67395824=47149² 1a76392485=47257² 
249a758631=52071² 2719634a85=54457² 286a795314=55979² 314672a895=59597² 3671a89245=632a4² 
376198a254=64069² 41697528a3=68335² 46928a7153=71485² 5a79286413=81196² 632a741859=83608² 
6713498a25=86074² 7148563a29=89468² 76315982a4=91429² 795186a234=93319² 983251a764=a3a39² 
   task 12
23 penholodigital squares in base 12                                                                     
135b7482a69=117789² 23a5b976481=16357 24ab5379861=16762 25386749ba1=16906 26b859a3714=173434² 
2835ba17694=178278² 34a8125b769=1a1993² 354a279b681=1a3595² 3824b7569a1=1b0451² 3a5b2487961=1b7545² 
42a1583b769=2084a9² 5287ba13469=235273² 5b23a879641=2528b5² 62937b5a814=25b564² 63a8527b194=262174² 
73b615a8294=285a44² 7b9284a5361=29a977² 83ab5479261=2a7617² 8617b35a294=2b0144² 93825a67b41=307381² 
96528ab7314=310828² 9ab65823714=319488² 9b2573468a1=319a37²                                           
   stretch 13
0 penholodigital squares in base 13
   stretch 14
160 penholodigital squares in base 14           
126a84d79c53b=1129535² ...                     
db3962a7541c8=3a03226²                         
   stretch 15
419 penholodigital squares in base 15             
12378da5b6ec94=4240c58² ...                      
ed4c93285671ba=ee25e4a²                
   stretch 16
740 penholodigital squares in base 16                 
123da7f85bce964=11156eb6² ...                        
fec81b69573da24=3fd8f786²                                      
   NB. this is getting to be obnoxiously long in terms of time...

Raku

(9 .. 12).map: -> $base {
    my $test = (1 ..^ $base)».base($base).join;
    my $start = $test     .parse-base($base).sqrt.Int;
    my $end   = $test.flip.parse-base($base).sqrt.Int;
    say "\nThere is a total of {+$_} penholodigital squares in base $base:\n" ~
        .map({"{.base($base)}² = {.².base($base)}"}).batch(3)».join(", ").join: "\n" given
        ($start .. $end).grep: *².base($base).comb.sort.join eq $test
}

(13 .. 16).hyper(:1batch).map: -> $base {
    my $test = (1 ..^ $base)».base($base).join;
    my $start = $test     .parse-base($base).sqrt.Int;
    my $end   = $test.flip.parse-base($base).sqrt.Int;
    my @penholo = ($start .. $end).grep: *².base($base).comb.sort.join eq $test;
    say "\nThere is a total of {+@penholo} penholodigital squares in base $base:";
    say @penholo[0,*-1].map({"{.base($base)}² = {.².base($base)}"}).batch(3)».join(", ").join: "\n" if +@penholo;
}
Output:
There is a total of 10 penholodigital squares in base 9:
3825² = 16328547, 3847² = 16523874, 4617² = 23875614
4761² = 25487631, 6561² = 47865231, 6574² = 48162537
6844² = 53184267, 7285² = 58624317, 7821² = 68573241
8554² = 82314657

There is a total of 30 penholodigital squares in base 10:
11826² = 139854276, 12363² = 152843769, 12543² = 157326849
14676² = 215384976, 15681² = 245893761, 15963² = 254817369
18072² = 326597184, 19023² = 361874529, 19377² = 375468129
19569² = 382945761, 19629² = 385297641, 20316² = 412739856
22887² = 523814769, 23019² = 529874361, 23178² = 537219684
23439² = 549386721, 24237² = 587432169, 24276² = 589324176
24441² = 597362481, 24807² = 615387249, 25059² = 627953481
25572² = 653927184, 25941² = 672935481, 26409² = 697435281
26733² = 714653289, 27129² = 735982641, 27273² = 743816529
29034² = 842973156, 29106² = 847159236, 30384² = 923187456

There is a total of 20 penholodigital squares in base 11:
42045² = 165742A893, 43152² = 173A652894, 44926² = 18792A6453
47149² = 1A67395824, 47257² = 1A76392485, 52071² = 249A758631
54457² = 2719634A85, 55979² = 286A795314, 59597² = 314672A895
632A4² = 3671A89245, 64069² = 376198A254, 68335² = 41697528A3
71485² = 46928A7153, 81196² = 5A79286413, 83608² = 632A741859
86074² = 6713498A25, 89468² = 7148563A29, 91429² = 76315982A4
93319² = 795186A234, A3A39² = 983251A764

There is a total of 23 penholodigital squares in base 12:
117789² = 135B7482A69, 16357B² = 23A5B976481, 16762B² = 24AB5379861
16906B² = 25386749BA1, 173434² = 26B859A3714, 178278² = 2835BA17694
1A1993² = 34A8125B769, 1A3595² = 354A279B681, 1B0451² = 3824B7569A1
1B7545² = 3A5B2487961, 2084A9² = 42A1583B769, 235273² = 5287BA13469
2528B5² = 5B23A879641, 25B564² = 62937B5A814, 262174² = 63A8527B194
285A44² = 73B615A8294, 29A977² = 7B9284A5361, 2A7617² = 83AB5479261
2B0144² = 8617B35A294, 307381² = 93825A67B41, 310828² = 96528AB7314
319488² = 9AB65823714, 319A37² = 9B2573468A1

There is a total of 0 penholodigital squares in base 13:

There is a total of 160 penholodigital squares in base 14:
1129535² = 126A84D79C53B, 3A03226² = DB3962A7541C8

There is a total of 419 penholodigital squares in base 15:
4240C58² = 12378DA5B6EC94, EE25E4A² = ED4C93285671BA

There is a total of 740 penholodigital squares in base 16:
11156EB6² = 123DA7F85BCE964, 3FD8F786² = FEC81B69573DA24

Wren

Library: Wren-math
Library: Wren-fmt

This is limited to base 14 as base 15 would overflow Wren's safe integer limit of 2^53.

import "./math" for Int
import "./fmt" for Conv, Fmt

var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]
var digits = "123456789ABCD"

for (b in 9..14) {
    var master = 1
    for (d in 1...b) master = master * primes[d-1]
    var phd = []
    var max = Conv.atoi(digits[(b-2)..0].join(), b).sqrt.floor
    for (i in 1..max) {
        var sq = i * i
        if (b == 10 && (sq % 3) != 0) continue
        var digs = Int.digits(sq, b)
        if (digs.contains(0)) continue
        var key = 1
        for (dig in digs) key = key * primes[dig-1]
        if (key == master) phd.add(i)
    }
    System.print("There is a total of %(phd.count) penholodigital squares in base %(b):")
    if (b > 13) phd = [phd[0], phd[-1]]
    for (i in 0...phd.count) {
        Fmt.write("$s² = $s  ", Conv.Itoa(phd[i], b), Conv.Itoa(phd[i] * phd[i], b))
        if ((i + 1) % 3 == 0) System.print()
    }
    if (phd.count % 3 != 0) System.print()
    System.print()
}
Output:
There is a total of 10 penholodigital squares in base 9:
3825² = 16328547  3847² = 16523874  4617² = 23875614  
4761² = 25487631  6561² = 47865231  6574² = 48162537  
6844² = 53184267  7285² = 58624317  7821² = 68573241  
8554² = 82314657  

There is a total of 30 penholodigital squares in base 10:
11826² = 139854276  12363² = 152843769  12543² = 157326849  
14676² = 215384976  15681² = 245893761  15963² = 254817369  
18072² = 326597184  19023² = 361874529  19377² = 375468129  
19569² = 382945761  19629² = 385297641  20316² = 412739856  
22887² = 523814769  23019² = 529874361  23178² = 537219684  
23439² = 549386721  24237² = 587432169  24276² = 589324176  
24441² = 597362481  24807² = 615387249  25059² = 627953481  
25572² = 653927184  25941² = 672935481  26409² = 697435281  
26733² = 714653289  27129² = 735982641  27273² = 743816529  
29034² = 842973156  29106² = 847159236  30384² = 923187456  

There is a total of 20 penholodigital squares in base 11:
42045² = 165742A893  43152² = 173A652894  44926² = 18792A6453  
47149² = 1A67395824  47257² = 1A76392485  52071² = 249A758631  
54457² = 2719634A85  55979² = 286A795314  59597² = 314672A895  
632A4² = 3671A89245  64069² = 376198A254  68335² = 41697528A3  
71485² = 46928A7153  81196² = 5A79286413  83608² = 632A741859  
86074² = 6713498A25  89468² = 7148563A29  91429² = 76315982A4  
93319² = 795186A234  A3A39² = 983251A764  

There is a total of 23 penholodigital squares in base 12:
117789² = 135B7482A69  16357B² = 23A5B976481  16762B² = 24AB5379861  
16906B² = 25386749BA1  173434² = 26B859A3714  178278² = 2835BA17694  
1A1993² = 34A8125B769  1A3595² = 354A279B681  1B0451² = 3824B7569A1  
1B7545² = 3A5B2487961  2084A9² = 42A1583B769  235273² = 5287BA13469  
2528B5² = 5B23A879641  25B564² = 62937B5A814  262174² = 63A8527B194  
285A44² = 73B615A8294  29A977² = 7B9284A5361  2A7617² = 83AB5479261  
2B0144² = 8617B35A294  307381² = 93825A67B41  310828² = 96528AB7314  
319488² = 9AB65823714  319A37² = 9B2573468A1  

There is a total of 0 penholodigital squares in base 13:

There is a total of 160 penholodigital squares in base 14:
1129535² = 126A84D79C53B  3A03226² = DB3962A7541C8