Subject: extracting the first N digits of a number
From: Barton Willis
Date: Thu, 21 Jul 2005 13:51:07 -0500
I think you asked about the digits of a float;
nevertheless, this (might be) is amusing:
(%i1) load("C:/maxima/nummod/nummod.lisp")$
(%i2) load("C:/maxima/friendly.mac")$
The number 6 is perfect and nonfriendly:
(%i3) friendly_sequence(6,10);
(%o3) [6,36,45,41,17,50,25,29,85,89,145]
The number 28 is perfect and friendly:
(%i4) friendly_sequence(28,10);
(%o4) [28,68,100,1,1,1,1,1,1,1,1]
The numbers 153 and 54748 are narcissistic:
(%i5) narcissistic_numberp(153,3);
(%o5) true
(%i6) narcissistic_numberp(54748,5);
(%o6) true
List the digits of a nonnegative interger:
(%i7) digitlist(0);
(%o7) [0]
(%i8) digitlist(10);
(%o8) [1,0]
(%i9) digitlist(100);
(%o9) [1,0,0]
(%i10) digitlist(101);
(%o10) [1,0,1]
(%i11)
----- friendly.mac -----
digitlist(n) := if integerp(n) and n > -1 then digitlist_helper(n,[]) else
error("Input to 'digitlist' must be a nonnegative integer");
digitlist_helper(n,acc) := block([p],
if n < 10 then cons(n,acc) else (
p : nummod(n,10),
digitlist_helper(floor((n-p)/10), cons(p,acc))));
friendly_sequence(p,n) := block([acc : [p],listarith : true],
for i : 1 thru n do (
p : apply("+", digitlist(p)^2),
acc : endcons(p,acc)),
acc);
narcissistic_numberp(n,k) := block([listarith : true],
is (n = apply("+",digitlist(n)^k)));
---------------
Barton