I am fan of numbers, so obsessed that in my teen age i found , on my own, how to fill a magic square of odd length.

I am fan of TSQL and prefer writing tsql over c# or other coding language.

This is not my homework nor my company work.

This is just for fun.

I love Narcissistic Number so much so that my cell# is 0331-9307774

(where 0331 is service provider number

and there is no service provider whoose number starts with 467)

**4679307774** is the only Narcissistic Number in 10 digits length.

I am trying to find this number with TSQL.

but the tsql has compilation error

the error is

Arithmetic overflow error for type int, value = 3486784401.000000.

**What I want**

**a) remove the compilation / syntax error.**

**b) suggestion as how to produce armstrong number in TSQL as fast as possible.**

here is the code with syntax error

Arithmetic overflow error for type int, value = 3486784401.000000.

```
select Number=a*1000000000 + b*100000000 + c*10000000 + d*1000000 + e*100000 + f*10000 + g*1000 + h*100 + i*10+ j
from
(select a=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)a
cross join
(select b=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)b
cross join
(select c=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)c
cross join
(select d=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)d
cross join
(select e=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)e
cross join
(select f=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)f
cross join
(select g=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)g
cross join
(select h=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)h
cross join
(select i=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)i
cross join
(select j=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)j
where a*1000000000 + b*100000000 + c*10000000 + d*1000000 + e*100000 + f*10000 + g*1000 + h*100+ i*10+ j
= power(a,10) + power(b,10) + power(c,10) + power(d,10) + power(e,10) + power(f,10) + power(g,10) + power(h,10) + power(i,10) + power(j,10)
and (a*1000000000 + b*100000000 + c*10000000 + d*1000000 + e*100000 + f*10000 + g*1000 + h*100 + i*10+ j)> 1000000000
order by 1
```

this code produces 1st 15 armstrong numbers

```
select Number=a*100 + b*10 + c
from
(select a=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)a
cross join
(select b=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)b
cross join
(select c=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)c
where a*100 + b*10 + c = power(a,3) + power(b,3) + power(c,3)
and (a*100 + b*10 + c )> 100
union all
select Number=a*1000 + b*100 + c*10 + d
from
(select a=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)a
cross join
(select b=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)b
cross join
(select c=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)c
cross join
(select d=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)d
where a*1000 + b*100 + c*10 + d = power(a,4) + power(b,4) + power(c,4) + power(d,4)
and (a*1000 + b*100 + c*10 + d)> 1000
union all
select Number=a*10000 + b*1000 + c*100 + d*10 + e
from
(select a=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)a
cross join
(select b=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)b
cross join
(select c=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)c
cross join
(select d=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)d
cross join
(select e=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)e
where a*10000 + b*1000 + c*100 + d*10 + e = power(a,5) + power(b,5) + power(c,5) + power(d,5) + power(e,5)
and (a*10000 + b*1000 + c*100 + d*10 + e)> 10000
union all
select Number=a*100000 + b*10000 + c*1000 + d*100 + e*10 + f
from
(select a=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)a
cross join
(select b=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)b
cross join
(select c=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)c
cross join
(select d=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)d
cross join
(select e=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)e
cross join
(select f=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)f
where a*100000 + b*10000 + c*1000 + d*100 + e*10 + f
= power(a,6) + power(b,6) + power(c,6) + power(d,6) + power(e,6) + power(f,6)
and (a*100000 + b*10000 + c*1000 + d*100 + e*10 + f)> 100000
union all
select Number=a*1000000 + b*100000 + c*10000 + d*1000 + e*100 + f*10 + g
from
(select a=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)a
cross join
(select b=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)b
cross join
(select c=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)c
cross join
(select d=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)d
cross join
(select e=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)e
cross join
(select f=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)f
cross join
(select g=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)g
where a*1000000 + b*100000 + c*10000 + d*1000 + e*100 + f*10 + g
= power(a,7) + power(b,7) + power(c,7) + power(d,7) + power(e,7) + power(f,7) + power(g,7)
and (a*1000000 + b*100000 + c*10000 + d*1000 + e*100 + f*10 + g)> 1000000
order by 1
```

Number

153

370

371

407

1634

8208

9474

54748

92727

93084

548834

1741725

4210818

9800817

9926315

this code produces the three possible numbers in length 8

```
select Number=a*10000000 + b*1000000 + c*100000 + d*10000 + e*1000 + f*100 + g*10 + h
from
(select a=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)a
cross join
(select b=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)b
cross join
(select c=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)c
cross join
(select d=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)d
cross join
(select e=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)e
cross join
(select f=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)f
cross join
(select g=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)g
cross join
(select h=0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)h
where a*10000000 + b*1000000 + c*100000 + d*10000 + e*1000 + f*100 + g*10 + h
= power(a,8) + power(b,8) + power(c,8) + power(d,8) + power(e,8) + power(f,8) + power(g,8) + power(h,8)
and (a*10000000 + b*1000000 + c*100000 + d*10000 + e*1000 + f*100 + g*10 + h)> 10000000
order by 1
```

Number

24678050

24678051

88593477