Exponential digital sums

From Rosetta Code
Revision as of 21:04, 10 August 2023 by Thundergnat (talk | contribs) (New draft task and Raku example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Exponential digital sums 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.

Some integers have the property that the digital sum of that integer raised to some integer power greater than 1, is equal to the original integer.


E.G.
92 == 81 8 + 1 == 9

Some integers have this property using more than one exponent.

183 == 5832 5 + 8 + 3 + 2 == 18
186 == 34012224 3 + 4 + 0 + 1 + 2 + 2 + 2 + 4 == 18
187 == 612220032 6 + 1 + 2 + 2 + 2 + 0 + 0 + 3 + 2 == 18

Note: every integer has an exponential digital sum equal to the original integer when using an exponent of 1.


Task
  • Find and show the first twenty integers (with their exponents), that satisfy this condition.
  • Find and show at least the first ten integers with their exponents, that satisfy this condition in three or more ways.


Raku

Implement a lazy generator. Made some assumptions about search limits. May be poor assumptions, but haven't been able to find any counterexamples.

my $start = 2;
my @expsum = lazy (2..*).hyper.map( -> $Int {
    my atomicint $miss = 0;
    ($start..$Int).map( -> $exp {
        if (my $sum = ($Int ** $exp).comb.sum) > $Int {
            ++⚛$miss;
            $start = $exp.Int - 30 max 2 unless $Int.ends-with: 0;
            last if $miss > 20;
        }
        $sum == $Int ?? "$Int^$exp" !! Empty;
    }) || Empty;
});

say "First twenty-five integers that are equal to the digital sum of that integer raised to some power:";
put .join(', ') for @expsum[^25];
say "\nFirst thirty that satisfy that condition in three or more ways:";
put .join(', ') for @expsum.grep({.elems >=3}).[^30];
Output:
First twenty-five integers that are equal to the digital sum of that integer raised to some power:
7^4
8^3
9^2
17^3
18^3, 18^6, 18^7
20^13
22^4
25^4
26^3
27^3, 27^7
28^4, 28^5
31^7
34^7
35^5
36^4, 36^5
40^13
43^7
45^6
46^5, 46^8
53^7
54^6, 54^8, 54^9
58^7
63^8
64^6
68^7

First thirty that satisfy that condition in three or more ways:
18^3, 18^6, 18^7
54^6, 54^8, 54^9
90^19, 90^20, 90^21, 90^22, 90^28
107^11, 107^13, 107^15
181^16, 181^18, 181^19, 181^20
360^45, 360^46, 360^49, 360^51
370^48, 370^54, 370^57, 370^59
388^32, 388^35, 388^36
523^39, 523^42, 523^44, 523^45
603^44, 603^47, 603^54
667^48, 667^54, 667^58
1134^78, 1134^80, 1134^82, 1134^86
1827^121, 1827^126, 1827^131
1828^123, 1828^127, 1828^132
2116^140, 2116^143, 2116^147
2330^213, 2330^215, 2330^229
2430^217, 2430^222, 2430^223, 2430^229, 2430^230
2610^228, 2610^244, 2610^246
2656^170, 2656^172, 2656^176
2700^406, 2700^414, 2700^420, 2700^427
2871^177, 2871^189, 2871^190
2934^191, 2934^193, 2934^195
3231^203, 3231^207, 3231^209
3448^215, 3448^221, 3448^227
3555^213, 3555^224, 3555^225
3727^227, 3727^232, 3727^236
3879^233, 3879^238, 3879^250
4483^266, 4483^273, 4483^274
5230^420, 5230^422, 5230^423
5500^693, 5500^711, 5500^714, 5500^717, 5500^721