Weekly Challenges

Solve the challenge, share your solution and summit the ranks of our Community!

Also available in | Français | Português | Español | 日本語
IDEAS WANTED

Want to get involved? We're always looking for ideas and content for Weekly Challenges.

SUBMIT YOUR IDEA

Challenge #187: Generate Prime Numbers

ericmartz
5 - Atom

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.

cam_w
11 - Bolide

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)


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

 

 

ZenonH
8 - 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
mccartycp
7 - Meteor
Spoiler
 
matthewbechard
8 - Asteroid

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.

Niklas
8 - Asteroid

🙂 

Bdonahue
7 - 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
laeorn
6 - Meteoroid
Spoiler
187.jpg

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

SueDonim
8 - Asteroid

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
Swaathi
5 - 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.