{ An n-digit number which is the sum of the n-th powers of its digits is called an n-narcissistic number. The smallest example other than the trivial 1-digit numbers is (n=3) 153 = 1*1*1 + 5*5*5 + 3*3*3 There are 88 known (and theoretically possible) n-narcissistic numbers, the largest with n = 39. For a very nice descrition of narcissistic numbers see http://mathworld.wolfram.com/NarcissisticNumber.html Here we present sample code to calculate all narcissistic numbers of 3 digits up to 8 digits. It is easy to generalize the code to calculate all 88 numbers, but beware the it can take VERY long time to calculate. Sample query: To calculate all 6 digit narcissistic numbers, run the query all Narcissistic(6) } Digit = L[0..9] DigitNZ = L[1..9] //No leading zero pred Narcissistic(n:<I[3..8]) iff if n = 3 then Narcissistic3() elsif n = 4 then Narcissistic4() elsif n = 5 then Narcissistic5() elsif n = 6 then Narcissistic6() elsif n = 7 then Narcissistic7() elsif n = 8 then Narcissistic8() end pred Narcissistic3() iff a::DigitNZ & b::Digit & c::Digit & 100*a + 10*b + c = Pow(3,a) + Pow(3,b) + Pow(3,c) & Print(a,b,c) pred Narcissistic4() iff a::DigitNZ & b::Digit & c::Digit & d::Digit & 1000*a + 100*b + 10*c + d = Pow(4,a) + Pow(4,b) + Pow(4,c) + Pow(4,d) & Print(a,b,c,d) pred Narcissistic5() iff a::DigitNZ & b::Digit & c::Digit & d::Digit & e::Digit & 10000*a + 1000*b + 100*c + 10*d + e = Pow(5,a) + Pow(5,b) + Pow(5,c) + Pow(5,d) + Pow(5,e) & Print(a,b,c,d,e) pred Narcissistic6() iff a::DigitNZ & b::Digit & c::Digit & d::Digit & e::Digit & f::Digit & 100000*a + 10000*b + 1000*c + 100*d + 10*e + f = Pow(6,a) + Pow(6,b) + Pow(6,c) + Pow(6,d) + Pow(6,e) + Pow(6,f) & Print(a,b,c,d,e,f) pred Narcissistic7()iff a::DigitNZ & b::Digit & c::Digit & d::Digit & e::Digit & f::Digit & g::Digit & 1000000*a + 100000*b + 10000*c + 1000*d + 100*e + 10*f + g = Pow(7,a) + Pow(7,b) + Pow(7,c) + Pow(7,d) + Pow(7,e) + Pow(7,f) + Pow(7,g) & Print(a,b,c,d,e,f,g) pred Narcissistic8()iff a::DigitNZ & b::Digit & c::Digit & d::Digit & e::Digit & f::Digit & g::Digit & h::Digit & 10000000*a + 1000000*b + 100000*c + 10000*d + 1000*e + 100*f + 10*g + h = Pow(8,a) + Pow(8,b) + Pow(8,c) + Pow(8,d) + Pow(8,e) + Pow(8,f) + Pow(8,g) + Pow(8,h) & Print(a,b,c,d,e,f,g,h) // xout = xin**e local proc Pow(e:<I,xin:<Digit,xout:>L) iff if e = 1 then xout = xin else Pow(e-1,xin,xout1) & xout = xin*xout1 end
This page was created by F1toHTML