community
cancel
Showing results for 
Search instead for 
Did you mean: 
Do you have the skills to make it to the top? Subscribe to our weekly challenges. Try your best to solve the problem, share your solution, and see how others tackled the same problem. We share our answer too.
Weekly Challenge
Do you have the skills to make it to the top? Subscribe to our weekly challenges. Try your best to solve the problem, share your solution, and see how others tackled the same problem. We share our answer too.
Unable to display your progress at this time. Please try again a little later, or contact an administrator if you continue to see this error.
Getting started with Designer? | Start your journey with our new Learning Path!

Challenge #187: Generate Prime Numbers

This is my first weekly challenge.  I think I did what several other did, which is to code in the conditions to test for a prime number.  My conditions go up to 100.

Fireball

My 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

 

 

Spoiler
#################################
# 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)


#################################

 

 

Asteroid

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. 

 

Spoiler
ZHSOL.png
Spoiler
 
Meteoroid

Here's my 1st published solution!187_challenge.png

 

I generated 50 rows, appended it to itself. I then used a formula to divide both sets of rows, split the result so I could filter on rows that have a .00000 in the decimal, and did a few other tricks.

Asteroid

 

Meteor

Brute Force

Spoiler
Generated rows for all numbers between 1 and X (50).  Created a macro to group inputs by number and all numbers.  Appended all numbers to each number and took the Modulo =0.  From there I summarized how many Polynomials there were.  If it was more than  (1 and the number itself) I filtered it out.  Returned the numbers to the main workflow.  
Screen Shot 2019-10-30 at 3.19.09 PM.pngScreen Shot 2019-10-30 at 3.18.59 PM.png
Atom
Spoiler
187.jpg

Fun challenge on my day off! Approach is detailed in the screenshot.

Alteryx Partner

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.

 

Spoiler
Process:
- 4 consecutive filters checking modulus when using first 4 primes (2, 3, 5, and 7).
- Add back those 4 primes since they get filtered out
- Clean-up

MySolution.PNG
Atom
Spoiler

Capture_PrimeNumbers_WC187.JPG

Uploading my solution. Have excluded 1 from divisors so that a number with a single divisor is considered as Prime number.