This site uses different types of cookies, including analytics and functional cookies (its own and from other sites). To change your cookie settings or find out more, click here. If you continue browsing our website, you accept these cookies.
We're actively looking for ideas on how to improve Weekly Challenges and would love to hear what you think!
Submit FeedbackMy solution uses the Python tool to implement the AKS Test for primes (slower method), because I love the Numberphile youtube channel ... https://youtu.be/HvMSRWTE2mI
All credit goes to the original authors of the 2002 paper, of course! https://www.cse.iitk.ac.in/users/manindra/algebra/primality_v6.pdf
#################################
# List all non-standard packages to be imported by your
# script here (only missing packages will be installed)
# from ayx import Package
# Package.installPackages(['pandas','numpy'])
#################################
from ayx import Alteryx
import numpy as np
import pandas as pd
import sys
#################################
def is_prime(p):
roots = [1 for _ in range(p)]
roots
poly1 = np.poly1d(roots, True)
poly1
coef = [1]
for _ in range(p-1):
coef.append(0)
coef.append(-1)
coef
poly2 = np.poly1d(coef, False)
poly2
poly3 = poly1 - poly2
poly3
return np.array_equal(poly3.c[:p-1]%p, np.zeros(p-1))
#################################
dfInput = Alteryx.read("#1")
dfAnswer = Alteryx.read("#2")
#################################
results = []
for i in range(2, dfInput['Number'][0]+1):
if is_prime(i):
results.append(i)
results
#################################
dfOutput = pd.DataFrame(results, columns=['Number'])
#################################
dfAnswer = dfAnswer.astype('int64')
#################################
if dfAnswer.equals(dfOutput):
print("SUCCESS!!! Found all primes")
else:
sys.exit("ERROR!!! Try again")
#################################
Alteryx.write(dfOutput, 1)
#################################
I learned a few things about Alteryx with this challenge. First off no Factorial function, and secondly that the mod function doesn't like numbers roughly 1124000727777607680000 and larger.
I first tried to use Wilson's Theorem to solve this challenge, but quickly found challenge #74, which asks users to create a factorial calculator, persistent I used wolfram alpha to build a table for 2->50!. After doing so I couldn't get my formula tool to process mod(1124000727777607680000,22) or beyond, so I looked to a less calculation intense solution.
Brute Force
Fun challenge on my day off! Approach is detailed in the screenshot.
My answer was essentially the same as #Jean-Balteryx though not as compact. Feels a bit brute force, and certainly not a repeatable process. I actually like #OllieClarke's approach quite a bit. That was kind of what I was thinking, but I didn't quite get there.
Uploading my solution. Have excluded 1 from divisors so that a number with a single divisor is considered as Prime number.