Project Euler Problem #277 I am trying to solve Euler project problem 277 using python 3.8. First, I will show my program and than ask specific questions for enhancements.
The problem is a modified version of Collatz sequence. The general rule of the new one is:

\$ a_{n+1} = \, \,\, \frac {a_n} 3 \quad\$  if \$ a_n \$  is
divisible by 3. We shall denote this as a large downward step, &quot;D&quot;.

\$ a_{n+1} = \frac {4 a_n+2} 3 \, \, \$ if \$ a_n \$   divided by 3
gives  a remainder of \$ 1 \$ . We shall denote this as an upward
step, &quot;U&quot;.

\$ a_{n+1} = \frac {2 a_n -1} 3 \, \, \$ if \$ a_n \$   divided by 3
gives a remainder of \$ 2 \$ . We shall denote this as a small
downward step, &quot;d&quot;.


For instance if \$ a_1=231\$ , then the sequence \$ \{a_n\}=\{231,77,51,17,11,7,10,14,9,3,1\} \$ corresponds to the steps &quot;DdDddUUdDD&quot;.
The problem asks What is the smallest \$ a_1 &gt; 10^{15}\$ that begins with the sequence &quot;UDDDUdddDDUDDddDdDddDDUDDdUUDd&quot;?
My code:
def modifCollatz(x, seq, num):
   if num == 0:
       return seq

   if x % 3 == 0:
       num -= 1
       x = x / 3
       seq.append(&quot;D&quot;)
       return modifCollatz(x, seq, num)

   elif x % 3 == 1:
       num -= 1
       x = (4 * x + 2) / 3
       seq.append(&quot;U&quot;)
       return modifCollatz(x, seq, num)

   else:
       x = (2 * x - 1) / 3
       num -= 1
       seq.append(&quot;d&quot;)
       return modifCollatz(x, seq, num)


wordList = []
for i in &quot;UDDDUdddDDUDDddDdDddDDUDDdUUDd&quot;:
   wordList.append(i)

n = 10 ** 15
while True:
   if modifCollatz(n, [], 30)[0:30] == wordList:
       print(n)
       break
   else:
       n += 3


That works. I have tested using the example given in problem 277.

I am aware that it's using brute force and don't know what can be done to improve efficiency. I was just able to think of adding solely finding the first 30 elements of each number and incrementing by 3 n += 3, which are probably the easiest ones. What can be done additionally?
Creating a list out of a string
wordList = []
for i in &quot;UDDDUdddDDUDDddDdDddDDUDDdUUDd&quot;:
   wordList.append(i)

can be shortened to:
wordList = list(&quot;UDDDUdddDDUDDddDdDddDDUDDdUUDd&quot;)

Algorithm
The first idea is to check the sequence letter by letter. This is more efficient than generating the sequence for a number x and then see if it matches the target sequence.
To find the number that matches Dd starting from 3, it would be:

x = 3
3 % 3 == 0, so it matches D
x = 3 / 3 = 1
1 doesn't match d, so try next number
x = 3 + 1 = 4
4 doesn't match D
x = 4 + 1 = 5
5 doesn't match D
x = 5 + 1 = 6
6 matches D
x = 6 / 3 = 2
2 matches d
Solution is 6

Second idea is to filter out numbers that cannot be part of the solution.
In the previous example, numbers 4 and 5 can be filtered out. Similarly to your n += 3. Let's see some examples:
Numbers that match the prefix D:
3,6,9,12,15,18,21...

Numbers that match the prefix DD:
9,18,27,36...

Numbers that match the prefix DDD:
27,54,81,...


Each sequence is a subset of the previous.
Numbers in the sequence differ by \$3^i\$, with i starting from 1.

Numbers that match the prefix U:
4,7,10,13,16,19,21,...

Numbers that match the prefix UU:
7,16,25,34,...

Numbers that match the prefix UUU:
25,52,79,...


Each sequence is a subset of the previous.
Numbers in the sequence differ by \$3^i\$, with i starting from 1.

Same for sequences that match d. Let's see a mixed prefix.
Numbers that match the prefix D:
3,6,9,12,15,18,21...

Numbers that match the prefix DU:
12,21,30,...


Each sequence is a subset of the previous.
Numbers in the sequence differ by \$3^i\$, with i starting from 1.