Skip to content

Add function to test for narcissistic numbers to csdb #18

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 Jul 21, 2023 · 3 comments
Closed

Add function to test for narcissistic numbers to csdb #18

delphidabbler opened this issue Jul 21, 2023 · 3 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

Following definition of narcissistic numbers (of any base > 1) from Wikipedia:

Screenshot_20230721-074955_Chrome

@delphidabbler delphidabbler self-assigned this Jul 21, 2023
@delphidabbler delphidabbler added enhancement New feature or request accepted Issue will be actioned labels Jul 21, 2023
@github-project-automation github-project-automation bot moved this to Considering in Code Snippets Jul 21, 2023
@delphidabbler delphidabbler moved this from Considering to Accepted in Code Snippets Jul 21, 2023
@delphidabbler
Copy link
Owner Author

delphidabbler commented Jul 24, 2023

function IsNarcissistic(const N: Cardinal; const B: Byte): Boolean;
begin
  if N < B then
    Exit(True); // trivial narcissistic numbers
  // number of digits in N 
  var K: Cardinal := Math.Floor(Math.LogN(B, N)) + 1;
  var Sum: Cardinal := 0;
  var BToPowerI := 1; // B to power I in 0..K-1
  for var I := 0 to K - 1 do
  begin
    var BToPowerIPlus1 := BToPowerI * B; // B to power I+1
    // calculate Ith digit
    var D := ((N mod BToPowerIPlus1) - (N mod BToPowerI)) div BToPowerI;
    // add D to power K to sum
    Inc(Sum, PowNZN(D, K));
    BToPowerI := BToPowerIPlus1;
  end;
  Result := N = Sum;
end;

@delphidabbler
Copy link
Owner Author

If we use DigitPowerSum from issue #17 we can reduce the function to:

function IsNarcissistic(const N: Cardinal; const B: Byte): Boolean;
begin
  if N < B then
    Exit(True); // trivial narcissistic numbers
  // number of digits in N 
  var K: Cardinal := Math.Floor(Math.LogN(B, N)) + 1;
  var Sum := DigitPowerSum(N, B, K);
  Result := N = Sum;
end;

The downside of this shortened version is that K gets calculated twice, once in DigitPowerSum and again in the above function.

@delphidabbler delphidabbler added the in progress Work has started on this issue label Jan 7, 2025
@delphidabbler delphidabbler moved this from Accepted to In progress in Code Snippets Jan 7, 2025
@delphidabbler
Copy link
Owner Author

Implemented by merge commit 47be72f

@delphidabbler delphidabbler removed accepted Issue will be actioned in progress Work has started on this issue labels Jan 9, 2025
@delphidabbler delphidabbler moved this from In progress to Completed in Code Snippets Jan 9, 2025
@delphidabbler delphidabbler added this to the Next Release milestone Jan 9, 2025
@delphidabbler delphidabbler added the completed Issue completed and committed to develop. To be closed on next release label Jan 10, 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