# Step 1: Input the sequence
sequence = [
20, 20, 40, 500, 100, 40, 100, 100, 100, 40, 100, 0, 100, 120, 40,
120, 20, 20, 120, 20, 120, 120, 100, 100, 20, 100, 20, 40, 20, 40,
120, 120, 20, 120, 40, 20, 20, 20, 20, 120, 20, 20, 500, 20, 120,
20, 40, 100, 40, 500
]
# Step 2: Calculate frequency of each number
from collections import Counter
frequency = Counter(sequence)
print("Frequency of each number:")
for number, count in frequency.items():
print(f"{number}: {count} times")
# Step 3: Build a Markov Chain model
from collections import defaultdict
# Dictionary to hold counts of transitions
transitions = defaultdict(lambda: defaultdict(int))
# Populate the transition counts
for i in range(len(sequence)-1):
current = sequence[i]
next_num = sequence[i+1]
transitions[current][next_num] += 1
# Convert counts to probabilities
transition_prob = {}
for current in transitions:
total = sum(transitions[current].values())
probs = {next_num: count / total for next_num, count in transitions[current].items()}
transition_prob[current] = probs
# Step 4: Predict the next number
last_number = sequence[-1]
if last_number in transition_prob:
next_probs = transition_prob[last_number]
# Choose the next number based on probabilities
import random
next_number = random.choices(
list(next_probs.keys()),
weights=list(next_probs.values()),
k=1
)[0]
else:
# If no transitions from last_number, choose randomly based on overall frequency
next_number = random.choices(
list(frequency.keys()),
weights=list(frequency.values()),
k=1
)[0]
print(f"Predicted next number: {next_number}")
Возможно ли на их основе с помощью какого нибудь программы вычислителя продолжить последовательность? Всего тут в данный момент 50 чисел, прошу помочь!!