How many 5-digit numbers satisfy the following conditions:
The number contains only digits 1, 2, 3, 4, 5.
The number starts with 1, 3, or 5.
Each pair of adjacent digits differs by exactly 1.
For example, 32343, 12345, 54545
Here's what I get......
1 2 1 2 1
1 2 1 2 3
1 2 3 2 1
1 2 3 2 3
1 2 3 4 3
1 2 3 4 5
3 2 1 2 1
3 2 1 2 3
3 2 3 2 1
3 2 3 2 3
3 4 3 4 3
3 4 5 4 5
5 4 3 2 1
5 4 3 2 3
5 4 3 4 3
5 4 3 4 5
5 4 5 4 3
5 4 5 4 5
Did I miss any ???
total nos=24.
but, some glitches in my code. can anyone spot it?
12121
12123
12321
12323
12343
12345
32121
32123
32321
32323
32343
32345
34321
34323
34343
34345
34543
34545
*34565
*34567
54321
54323
54343
54345
54543
54545
26
python code#
total=0
f=[0]*5
for i in range(12121,54546):
temp=i
for j in range(5):
f[j]=temp//(10**(4-j))
temp%=(10**(4-j))
#print(f[0])
if f[0] in range(1,6,2):
flag=0
for k in range(1,5):
if f[k]>0 and abs(f[k]-f[k-1])==1:
flag+=1
if(flag==4):
print(f[0]*10000+ f[1]*1000+ f[2]*100+ f[3]*10+ f[4])
total+=1
flag=0
print(total)