Skip to content

Commit 66e24e3

Browse files
committed
Add new DigitPowerSum routine to Maths category
Added new source code file. Updated maths.ini with meta data for DigitPowerSum. Also updated SeeAlso fields of related DigitSum and DigitSumBase routines.
1 parent 1af91b3 commit 66e24e3

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

collection/661.dat

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function DigitPowerSum(N: Integer; const Base: Byte; const Exponent: Byte):
2+
Int64;
3+
var
4+
SignOfN: Math.TValueSign;
5+
Digit: Integer;
6+
PowerDigit: Int64;
7+
begin
8+
if Base < 2 then
9+
raise SysUtils.EArgumentException.Create(
10+
'Base must be in the range 2..255'
11+
);
12+
if N = 0 then
13+
Exit(0);
14+
SignOfN := Math.Sign(N);
15+
N := Abs(N);
16+
Result := 0;
17+
repeat
18+
Digit := N mod Base;
19+
PowerDigit := PowNZN(Digit, Exponent);
20+
if High(Int64) - PowerDigit < Abs(Result) then
21+
raise SysUtils.EOverflow.Create('Overflow calculating digit power sum');
22+
Result := Result + PowerDigit;
23+
N := N div Base;
24+
until N = 0;
25+
if SignOfN = Math.NegativeValue then
26+
Result := -1 * Result;
27+
end;

collection/maths.ini

+16-2
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ Extra="<p>Sums of digits of negative numbers are negative, for example <mono>Dig
620620
TestInfo=advanced
621621
AdvancedTest.Level=unit-tests
622622
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
623-
SeeAlso=DigitSumBase
623+
SeeAlso=DigitSumBase,DigitPowerSum
624624
Snip=418.dat
625625
Delphi2=N
626626
Delphi3=N
@@ -1763,7 +1763,7 @@ DisplayName="DigitSumBase"
17631763
DescEx="<p>Calculates the sum of all the digits of integer <var>N</var> when epxressed in base <var>Base</var>. The returned value has the same sign as <var>N</var>.</p><p>Bases up to 255 are supported. If <var>Base</var> &lt; 2 then an <var>EArgumentException</var> exception is raised.</p>"
17641764
Kind=routine
17651765
Units=SysUtils,Math
1766-
SeeAlso=DigitSum
1766+
SeeAlso=DigitSum,DigitPowerSum
17671767
TestInfo=advanced
17681768
AdvancedTest.Level=unit-tests
17691769
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
@@ -1798,3 +1798,17 @@ AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tes
17981798
Snip=659.dat
17991799
DelphiXE=Y
18001800
Delphi12A=Y
1801+
1802+
[DigitPowerSum]
1803+
DisplayName=DigitPowerSum
1804+
DescEx="<p>Calculates the sum of all the digits of integer <var>N</var> in base <var>Base</var> where each digit is raised to the power <var>Exponent</var>. The returned value has the same sign as <var>N</var>.</p><p>If the result is too large to be represented as an <var>Int64</var> value then an <var>EOverflow</var> exception is raised.</p><p>Bases up to 255 are supported. If <var>Base</var> &lt;= 2 then an <var>EArgumentException</var> exception is raised.</p>"
1805+
Kind=routine
1806+
Units=SysUtils,Math
1807+
Depends=PowNZN
1808+
SeeAlso=DigitSum,DigitSumBase
1809+
TestInfo=advanced
1810+
AdvancedTest.Level=unit-tests
1811+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1812+
Snip=661.dat
1813+
DelphiXE=Y
1814+
Delphi12A=Y

0 commit comments

Comments
 (0)