Skip to content

Instantly share code, notes, and snippets.

@peteketcham
Created March 8, 2023 20:12
Show Gist options
  • Save peteketcham/515aebdce33a29c16cd60fca29d90a11 to your computer and use it in GitHub Desktop.
Save peteketcham/515aebdce33a29c16cd60fca29d90a11 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
Project Euler, Problem 596
https://projecteuler.net/problem=596
Let be T(r) the number of integer quadruplets x, y, z, t such that x**2 + y**2 + z**2 + t**2 ≤ r**2.
In other words, T(r) is the number of lattice points in the four-dimensional hyperball of radius r.
You are given that T(2) = 89, T(5) = 3121, T(100) = 493490641 and T(10**4) = 49348022079085897.
Find T(10**8) mod 1000000007.
"""
def brute_calc(in_r):
"""iterate through hypercube of radius r"""
ring = in_r**2
result = 0
for x_axis in range(-in_r, in_r + 1):
for y_axis in range(-in_r, in_r + 1):
for z_axis in range(-in_r, in_r + 1):
for t_axis in range(-in_r, in_r + 1):
if ring >= x_axis**2 + y_axis**2 + z_axis**2 + t_axis**2:
result += 1
return result
if __name__ == '__main__':
assert brute_calc(2) == 89
assert brute_calc(5) == 3121
assert brute_calc(100) == 493490641
assert brute_calc(104) == 49348022079085897
@peteketcham
Copy link
Author

Naïve solution to Project Euler problem 596
At testing 1 million coordinates per second, this would take... significantly longer than the age of the universe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment