# q programming challenge – Narcissistic Numbers

This is the second in an occasional series of q challenges we’ll post on the blog. Please get involved by posting your own solutions in the comments (prior to revealing the solution). As we get through the backlog of challenges that have been posed and completed by the AquaQ team, future challenges will be conducted ‘live’ on the blog, with the best solutions picked from the comments. ### Puzzle 2 – Narcissistic Numbers

153 is the smallest narcissistic number greater than 10. It’s called narcissistic because when the digits are raised to the power of the count of the digits, you get the original number that is,

1^3 + 5^3 + 3^3 = 1+125+27=153

So the challenge for is using q code, to tell me what is the sum of all the narcissistic numbers between 10 and 2 million inclusive, using the shortest, fastest, q code you can.

q programming challenge – Narcissistic Numbers

1. walter

there is a small bug in narc function, as it includes 7 in the final sum (due to 0=0 in til[n]=not[o<10 … could just fix by dropping, final statement being sum o 1_where til[n]=not[o<10]*o)

2. Jose Cambronero

Here’s a (probably) naive solution, where you string digits and parse as separate numbers

sum n where n=sum each (“I”\$”s) xexp’ count each s:string n:10+til 1+(`int\$2e6)-10

1. 1. 3. Akash

\ts {power:count each sn:string nn:1+9_til x;show nn where nn=sum each (xexp[-48+`int\$sn;power])}
2723j, 450785888j

around 2.7 seconds

1. 