I will roll a random number between 1 and 1000. If the number is prime, I will select the prime option. I will choose the perfect square option if it is a perfect square. If it is a perfect cube, then I will choose the perfect cube option. But, if it is both a perfect cube and a perfect square, I will select only the 4th option (both a perfect square and a perfect cube). If it is neither of these, I will reroll the number.
another stupid market with a correct answer.
Code:
N = 1000000
LOWER_BOUND = 1
UPPER_BOUND = 1000
import random
def is_prime(x):
if x == 1:
return False
if x == 2:
return True
i = 2
while i <= x ** 0.5:
if (x % i) == 0:
return False
i += 1
return True
def is_perfect_cube(x):
for i in range(0, round(x ** (1/3)) + 2):
if x == i ** 3:
return True
return False
def is_perfect_square(x):
for i in range(0, round(x ** (1/2)) + 2):
if x == i ** 2:
return True
return False
resultsDict = {"prime": 0, "perfect cube": 0, "perfect square": 0, "both": 0}
currentNum = random.randint(LOWER_BOUND, UPPER_BOUND)
for i in range(0, N):
currentNum = random.randint(LOWER_BOUND, UPPER_BOUND)
while not is_prime(currentNum) and not is_perfect_cube(currentNum) and not is_perfect_square(currentNum):
currentNum = random.randint(LOWER_BOUND, UPPER_BOUND)
if is_prime(currentNum):
resultsDict["prime"] += 1
if is_perfect_square(currentNum) and is_perfect_cube(currentNum):
resultsDict["both"] += 1
else:
if is_perfect_cube(currentNum):
resultsDict["perfect cube"] += 1
if is_perfect_square(currentNum):
resultsDict["perfect square"] += 1
print(resultsDict)
print("Percent both prime and perfect cube: " + str(resultsDict["both"] / N))
print("Percent prime: " + str(resultsDict["prime"] / N))
print("Percent perfect square: " + str(resultsDict["perfect square"] / N))
print("Percent perfect cube: " + str(resultsDict["perfect cube"] / N))
Result:
Percent both prime and perfect cube: 0.014431
Percent prime: 0.81612
Percent perfect square: 0.135715
Percent perfect cube: 0.033734
Fair values (more or less, I don't care):
Prime: 81.6%
Perfect square: 13.5%
Perfect cube: 3.3%
Both perfect square and perfect cube: 1.45%
Exact numbers if anyone's wondering:
168 primes
28 squares (no cubes)
7 cubes (no squares)
3 square and cubes
206 total