Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bdaae99

Browse files
Add 2 new routines to Maths category
Added IsPalindromic & IsNarcissistic function to Mathematics category Added new source code .dat file for the functions. Updated maths.ini with meta data for both functions.
1 parent fb53ba7 commit bdaae99

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

‎collection/660.dat

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function IsPalindromic(const N: Int64; const Base: Byte = 10): Boolean;
2+
var
3+
Digits: SysUtils.TBytes;
4+
Idx: Integer;
5+
PartitionSize: Integer;
6+
begin
7+
Digits := DigitsOf(N, Base); // raises exception for Base < 2
8+
Result := True;
9+
PartitionSize := Length(Digits) div 2;
10+
for Idx := 0 to Pred(PartitionSize) do
11+
if Digits[Idx] <> Digits[Length(Digits) - Idx - 1] then
12+
Exit(False);
13+
end;

‎collection/662.dat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function IsNarcissistic(N: Integer; const Base: Byte = 10): Boolean;
2+
var
3+
Sum: Int64;
4+
begin
5+
N := Abs(N);
6+
Sum := DigitPowerSum(N, Base, DigitCountBase(N, Base));
7+
Result := N = Sum;
8+
end;

‎collection/maths.ini

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,3 +1812,32 @@ AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tes
18121812
Snip=661.dat
18131813
DelphiXE=Y
18141814
Delphi12A=Y
1815+
1816+
[IsPalindromic]
1817+
DisplayName="IsPalindromic"
1818+
DescEx="<p>Checks if the absolute value of integer <var>N</var> is palindromic in base <var>Base</var>.</p><p>Bases up to 255 are supported. If <var>Base</var> &lt; 2 then an <var>EArgumentException</var> exception is raised.</p>"
1819+
Extra="<p>A number expressed in a specified base is palindromic if it remains unchanged when its digits are reversed. See <a href="https://en.m.wikipedia.org/wiki/Palindromic_number">Wikipedia</a> for a formal definition and examples.</p><p>Strictly speaking a palindromic number should be non-negative. However, <var>IsPalindromic</var> considers negative numbers to be palindromic if and only if their absolute value is palindromic.</p>"
1820+
Kind=routine
1821+
Units=SysUtils
1822+
Depends=DigitsOf
1823+
SeeAlso=IsNarcissistic
1824+
TestInfo=advanced
1825+
AdvancedTest.Level=unit-tests
1826+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1827+
Snip=660.dat
1828+
DelphiXE=Y
1829+
Delphi12A=Y
1830+
1831+
[IsNarcissistic]
1832+
DisplayName=IsNarcissistic
1833+
DescEx="<p>Checks if the absolute value of integer <var>N</var> is a narcissistic number in base <var>Base</var>.</p><p>Bases up to 255 are supported. If <var>Base</var> &lt;= 2 then an <var>EArgumentException</var> exception is raised. An <var>EOverflow</var> exception may be raised for large numbers and bases.</p>"
1834+
Extra="<p>A narcissistic number in a given number base is a number that is the sum of its own digits each raised to the power of the number of digits. See <a href="https://en.wikipedia.org/wiki/Narcissistic_number">Wikipedia</a> for a formal definition and examples.</p><p>Strictly speaking a palindromic number should be non-negative. However, <var>IsNarcissistic</var> considers negative numbers to be narcissistic if and only if their absolute value is narcissistic.</p>"
1835+
Kind=routine
1836+
Depends=DigitCountBase,DigitPowerSum
1837+
SeeAlso=IsPalindromic
1838+
TestInfo=advanced
1839+
AdvancedTest.Level=unit-tests
1840+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1841+
Snip=662.dat
1842+
DelphiXE=Y
1843+
Delphi12A=Y

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /