import math
import pandas as pd
import matplotlib.pyplot as plt
from itertools import combinations_with_replacement, permutations
# Define necessary functions
def primes_with_values(max_value, default_value):
primes = []
for num in range(2, max_value + 1):
if all(num % i != 0 for i in range(2, int(math.sqrt(num)) + 1)):
primes.append((num, default_value))
print(f"Primes with Values: {primes}") # Debug statement
return primes
def updated_value_table(primes_values, max_value):
print(f"Updated Value Table Input: {primes_values}") # Debug statement
return primes_values
def calculate_circumference(max_value):
# Calculate the side angle
side_angle = 360 / max_value
# Convert the side angle to radians for trigonometric functions
side_angle_rad = math.radians(side_angle)
# Calculate the side length using the internal length of 1 and the side angle
# Using the sine rule: side_length = 2 * sin(side_angle / 2)
side_length = 2 * math.sin(side_angle_rad / 2)
# Calculate the circumference by multiplying the side length by max_value
circumference = max_value * side_length
return circumference
def add_pi_accuracy_values(updated_table, max_value):
pi = math.pi
circumference = calculate_circumference(max_value)
for i, (prime, value) in enumerate(updated_table):
pi_accuracy = abs(pi - circumference)
updated_table[i] = (prime, value, pi_accuracy)
print(f"Updated Table with Pi Accuracy: {updated_table}") # Debug statement
return updated_table
def generate_sums(max_value):
sums_dict = {}
for i in range(2, int(max_value) + 1):
sums_dict[i] = [sum(comb) for comb in combinations_with_replacement(range(1, int(max_value) + 1), i)]
print(f"Sums Dict: {sums_dict}") # Debug statement
return sums_dict
def calculate_total_probabilities(sums_dict, updated_table):
total_probabilities = {}
for key, sums in sums_dict.items():
total_prob = 0
for sum_value in sums:
for prime, value, pi_accuracy in updated_table:
if sum_value == prime:
total_prob += value
total_probabilities[key] = total_prob
print(f"Total Probabilities: {total_probabilities}") # Debug statement
return total_probabilities
def add_bonus_probabilities(updated_table, total_probabilities):
for i, (prime, value, pi_accuracy) in enumerate(updated_table):
bonus_prob = total_probabilities.get(prime, 0)
updated_table[i] = (prime, value, pi_accuracy, bonus_prob)
print(f"Updated Table with Bonus Probabilities: {updated_table}") # Debug statement
return updated_table
def decay_probabilities(sums_dict, total_probabilities):
decay_probs = {}
for key, sums in sums_dict.items():
decay_prob = 0
for sum_value in sums:
decay_prob += total_probabilities.get(sum_value, 0)
decay_probs[key] = decay_prob
print(f"Decay Probabilities: {decay_probs}") # Debug statement
return decay_probs
def counter_rotations(updated_table, decay_probabilities):
for i, (prime, value, pi_accuracy, bonus_prob) in enumerate(updated_table):
counter_rotation = decay_probabilities.get(prime, 0)
updated_table[i] = (prime, value, pi_accuracy, bonus_prob, counter_rotation)
print(f"Updated Table with Counter Rotations: {updated_table}") # Debug statement
return updated_table
def generate_long_divisions(max_value):
divisions_dict = {}
for i in range(1, int(max_value) + 1):
divisions_dict[i] = [i / j for j in range(1, int(max_value) + 1)]
print(f"Long Divisions: {divisions_dict}") # Debug statement
return divisions_dict
def filter_valid_divisions(divisions_dict, prime_list):
prime_set = set(prime_list)
filtered_divisions = {}
for key, divisions in divisions_dict.items():
filtered_divisions[key] = [div for div in divisions if div in prime_set or div % 4 == 0]
print(f"Filtered Divisions: {filtered_divisions}") # Debug statement
return filtered_divisions
def calculate_pi_difference_rate(filtered_divisions, prime_list):
pi = math.pi
pi_difference_rate = {}
for key, divisions in filtered_divisions.items():
pi_diff_rate = 0
for div in divisions:
pi_diff_rate += abs(pi - div)
pi_difference_rate[key] = pi_diff_rate
print(f"Pi Difference Rate: {pi_difference_rate}") # Debug statement
return pi_difference_rate
def estimate_pi(updated_table, current_max_value):
pi_estimates = [value for prime, value, pi_accuracy, bonus_prob, counter_rotation in updated_table]
if not pi_estimates: # Check if pi_estimates is empty
return current_max_value + 1
if len(set(pi_estimates)) == 1: # If all values are the same
return current_max_value + 1
return sum(pi_estimates) / len(pi_estimates)
def find_best_max_value(current_max_value, max_search_value, pi_accuracy_values):
for x in range(1, max_search_value + 1):
potential_value = current_max_value + x
circumference = calculate_circumference(potential_value)
if abs(math.pi - circumference) in pi_accuracy_values:
return potential_value
return current_max_value
def next_generation_max_value(current_max_value, generations, max_search_value):
max_value = current_max_value
for _ in range(generations):
primes_values = primes_with_values(max_value, 1.0)
updated_table = updated_value_table(primes_values, int(max_value))
updated_table = add_pi_accuracy_values(updated_table, max_value)
sums_dict = generate_sums(max_value)
total_probabilities = calculate_total_probabilities(sums_dict, updated_table)
updated_table = add_bonus_probabilities(updated_table, total_probabilities)
decay_probs = decay_probabilities(sums_dict, total_probabilities)
updated_table = counter_rotations(updated_table, decay_probs)
divisions_dict = generate_long_divisions(max_value)
prime_list = [prime for prime, *_ in primes_values] # Extract the prime numbers
filtered_divisions = filter_valid_divisions(divisions_dict, prime_list)
pi_diff_rate = calculate_pi_difference_rate(filtered_divisions, prime_list)
max_value = estimate_pi(updated_table, max_value)
# Iterate to find best max value
pi_accuracy_values = [pi_accuracy for _, _, pi_accuracy, _, _ in updated_table]
max_value = find_best_max_value(current_max_value, max_search_value, pi_accuracy_values)
return max_value, updated_table, pi_diff_rate, total_probabilities
# Example Usage
max_value, generations, max_search_value = 6, 6, 100
final_max_value, final_table, pi_diff_rate, total_probabilities = next_generation_max_value(max_value, generations, max_search_value)
print(f"Final Max Value: {final_max_value}")
print(f"Pi Difference Rate: {pi_diff_rate}")
# Convert final_table to DataFrame and print it
df = pd.DataFrame(final_table, columns=['Prime', 'Value', 'Pi Accuracy', 'Bonus Prob', 'Counter Rotation'])
print(df)
# Convert total_probabilities to DataFrame and print it
prob_df = pd.DataFrame(list(total_probabilities.items()), columns=['Number', 'Total Probability'])
print(prob_df)
# Plotting
plt.figure(figsize=(12, 6))
# Plot Pi Accuracy vs Prime
plt.subplot(1, 2, 1)
plt.scatter(df['Prime'], df['Pi Accuracy'])
plt.xlabel('Prime')
plt.ylabel('Pi Accuracy')
plt.title('Pi Accuracy vs Prime')
# Plot Total Probability vs Number
plt.subplot(1, 2, 2)
plt.scatter(prob_df['Number'], prob_df['Total Probability'])
plt.xlabel('Number')
plt.ylabel('Total Probability')
plt.title('Total Probability vs Number')
plt.tight_layout()
plt.show()
Now what its supposed to do: I want to calculate the estimate of pi using the circumfrence of the biggest shape in a radius 1 circle. so for max_value=4 its a square.
then I have a few probabilities associated. ignore that. after the *; +; -; /; updates to the probabilities in updated_table, i want to calculate pi in a different way using the amazing pi formula. (prime/multiple_of_4 * prime_2/multiple_of_4 ……) but only an estimate because you can only use divisions found in the counter_rotation_table.
lastly i want to comapere the two estimates of pi and if the second way is more presice backwards calculate a new max_value if the first is the most precise the max_value just increases by one. lastly i want to view some graphs
this code didn’t work as expected. im new to coding so im having a hard time following and debugging the AI’s code. I think something is wrong with the logic.
next to this the Updated_values dont seem to include bonus_values decay_values or Counter_rotation_values (these should be updated in each generation). next to this im not sure how the code holds up. since i cant really follow the code yet.
I also would like to use larger imputs in the code but i cant really find when the runtime becomes exponetially too large to compute. so i would like to know how i could stream line the code and add some progress bar.
thanks for the help in advance.
Gegogabanana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.