Skip to content

Add DigitPowerSum routine #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
delphidabbler opened this issue Jan 8, 2025 · 2 comments
Closed

Add DigitPowerSum routine #39

delphidabbler opened this issue Jan 8, 2025 · 2 comments
Assignees
Labels
completed Issue completed and committed to develop. To be closed on next release enhancement New feature or request

Comments

@delphidabbler
Copy link
Owner

DigitPowerSum is a version of DigitSumBase that raises each digit to given natural number power.

function DigitPowerSum(X: UInt64; Base: Byte; Exponent: Cardinal): Cardinal;
begin
  Assert(Base > 1);
  if X = 0 then
    Exit(0);
  var K := Math.Floor(Math.LogN(X)); // digit count - 1
  Result := 0;
  var BToPowerI := 1; // B to power I when I = 0
  for var I := 0 to K do
  begin
    var BToPowerIPlus1 := Base * BToPowerI;
    var D := ((X mod BToPowerIPlus1) - (X mod BToPowerI)) div BToPowerI;
    Result := Result + PowNZN(D, Exponent);
    BToPowerI := BToPowerIPlus1;
  end;
end;

Or alternatively:

function DigitPowerSum(X: Int64; Base: Byte; Exponent: Cardinal): Integer; overload;
begin
  Assert(Base > 1);
  var UResult :≈ DigitSumBase(UInt64(Abs(X)), Base, Exponent);
  if UResult > MaxInt then
    raise EOutOfRange('DigitPowerSum: Result exceeds MaxInt');
  Result := Integer(UResult);
  if X < 0 then
    Result : -1 * Result;
end;
@delphidabbler delphidabbler self-assigned this Jan 8, 2025
@delphidabbler delphidabbler added enhancement New feature or request accepted Issue will be actioned labels Jan 8, 2025
@github-project-automation github-project-automation bot moved this to Considering in Code Snippets Jan 8, 2025
@delphidabbler delphidabbler moved this from Considering to Accepted in Code Snippets Jan 8, 2025
@delphidabbler
Copy link
Owner Author

This routine first proposed in issue #17.

@delphidabbler
Copy link
Owner Author

delphidabbler commented Jan 9, 2025

The routine was implemented, differently to the versions proposed above, by merge commit fb53ba7

@delphidabbler delphidabbler added completed Issue completed and committed to develop. To be closed on next release and removed accepted Issue will be actioned labels Jan 9, 2025
@delphidabbler delphidabbler moved this from Accepted to Completed in Code Snippets Jan 9, 2025
@delphidabbler delphidabbler added this to the Next Release milestone Jan 9, 2025
@delphidabbler delphidabbler removed this from the Next Release milestone Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed Issue completed and committed to develop. To be closed on next release enhancement New feature or request
Projects
Status: Completed
Development

No branches or pull requests

1 participant