All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Numbers
Restricted ternary numbers (Posted on 2023-07-24) Difficulty: 3 of 5
Evaluate the quantity of 4-digit numbers such that:
a. Use only 3 digits: 1,2 & 3.
b. None of its neighboring digits differ by 2.

Same question for numbers of 5 and 6 digits- still using only 1,2,3.

Enjoy!

No Solution Yet Submitted by Ady TZIDON    
No Rating

Comments: ( Back to comment list | You must be logged in to post comments.)
Solution Solution | Comment 1 of 4
Assuming we are using the set of digits {1,2,3} no matter how many digits there are, and assuming a 1 cannot follow a 3 and a 3 cannot follow a 1, the number of ways an n-digit number can be constructed is:  (for 4,5,6 in bold)
numbers =  [0, 3, 7, 17, 41, 99, 239, 577, 1393, 3363]
(for n in  0,1,2, ... 9   n-digit numbers)

Algorithm:  I built a series of lists, each list having one more digit than the last, using the final digit of the element of the prior list to determine what choices of extra digit could be concatenated.

4-digit solution set:
[1111, 1112, 1121, 1122, 1123, 1211, 1212, 1221, 1222, 1223, 1232, 1233, 2111, 2112, 2121, 2122, 2123, 2211, 2212, 2221, 2222, 2223, 2232, 2233, 2321, 2322, 2323, 2332, 2333, 3211, 3212, 3221, 3222, 3223, 3232, 3233, 3321, 3322, 3323, 3332, 3333]

---------  code  ---------

ch = [['1','2'], ['1','2','3'], ['2','3']]
s1 = ['1','2','3']


s2 = []
s3 = []
s4 = []
s5 = []
s6 = []
s7 = []
s8 = []
s9 = []
numbers = [0] # zero ways if there are zero digits

for a in s1:
    for nexdigit in ch[  int(a[-1]) - 1   ]:
        s2.append(a+nexdigit)

for a in s2:
    for nexdigit in ch[  int(a[-1]) - 1   ]:
        s3.append(a+nexdigit)
for a in s3:
    for nexdigit in ch[  int(a[-1]) - 1   ]:
        s4.append(a+nexdigit)
for a in s4:
    for nexdigit in ch[  int(a[-1]) - 1   ]:
        s5.append(a+nexdigit)
for a in s5:
    for nexdigit in ch[  int(a[-1]) - 1   ]:
        s6.append(a+nexdigit)
for a in s6:
    for nexdigit in ch[  int(a[-1]) - 1   ]:
        s7.append(a+nexdigit)
for a in s7:
    for nexdigit in ch[  int(a[-1]) - 1   ]:
        s8.append(a+nexdigit)
for a in s8:
    for nexdigit in ch[  int(a[-1]) - 1   ]:
        s9.append(a+nexdigit)

numbers.append(len(s1))
numbers.append(len(s2))
numbers.append(len(s3))
numbers.append(len(s4))
numbers.append(len(s5))
numbers.append(len(s6))
numbers.append(len(s7))
numbers.append(len(s8))
numbers.append(len(s9))

print(numbers)

# first method, above, from 0 to 9 digits
# second method, (verification)  below, more brute force 
#              way but this was much slower; up to 6 digits.

count = 0
valids = ['1','2','3']
valid = '123'

answers = [[] for i in range (7)]
for digits in range(1,7):
    for i in range(10**(digits-1), 10**digits):
        stri = str(i)
        countit = True
        for s in stri:
            if  valid.find(s) < 0:
                countit = False
        if '13' in stri or '31' in stri:
            countit = False
        if countit:
            count += 1
            answers[digits].append(i)
nnumbers = []
for a in answers:
    nnumbers.append(len(a))

print(nnumbers)

---
[0, 3, 7, 17, 41, 99, 239, 577, 1393, 3363]
[0, 3, 7, 17, 41, 99, 239]

Edited on July 24, 2023, 11:30 am
  Posted by Larry on 2023-07-24 11:28:32

Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (0)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information