a. How many 5-digit numbers exist where one of its digits equals the sum of all other digits?
b. What is the sum of all those numbers?
No leading zeroes.
In this case it is far easier to program than do the combinatorics.
I get: cnt tot 2658 109249191
program di
implicit none
integer i1,i2,i3,i4,i5,i6,icnt
integer*16 itot
icnt=0
itot=0
do i1=1,9
do i2=0,9
do i3=0,9
do i4=0,9
do i5=0,9
if(
1 i5+i2+i3+i4.eq.i1.or.
2 i1+i5+i3+i4.eq.i2.or.
3 i1+i2+i5+i4.eq.i3.or.
4 i1+i2+i3+i5.eq.i4.or.
5 i1+i2+i3+i4.eq.i5)then
print 1,i1,i2,i3,i4,i5
1 format(5i1)
icnt=icnt+1
itot=itot+ i1*10000+i2*1000+i3*100+i4*10+i5
endif
enddo
enddo
enddo
enddo
enddo
print*,' cnt tot',icnt,itot
end
this has been edited to correct a programming error pointed out later by Charlie.
Edited on September 16, 2018, 12:12 am
Edited on September 16, 2018, 12:13 am