Advent of Code is back! Unwrap daily challenges to sharpen your Alteryx skills and earn badges along the way! Learn more now.
Community is experiencing an influx of spam. As we work toward a solution, please use the 'Notify Moderator' option on the ellipsis menu to flag inappropriate posts.
Free Trial

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 #79: Find the Closest Prime Number

JoeM
Alteryx Alumni (Retired)

View last week's challenge HERE.

 

This week's challenge is simple to grasp, but more difficult to execute. This week, let's make an application that finds the closest prime number to a users input.

 OptimusPrime.gif

A prime number is a number that has two positive divisors - 1 and itself.

Without the use of a lookup table, please build an app that takes a user-entered number and returns the closest prime number to the entered number.

 

Fun fact:  this post went live on 07/31/2017 - a very prime day indeed!

 

MarqueeCrew
20 - Arcturus
20 - Arcturus

@JoeM,

 

I hope that I didn't make my life harder with this prime problem.  I created an application that on output answers your question, plus it states:

 

Is the number itself PRIME?

What is the previous largest PRIME number?

What is the next largest PRIME number?

 

On input I permit values up to 10,000.

 Capture.png

 

 

Cheers,

Mark

 

Alteryx ACE & Top Community Contributor

Chaos reigns within. Repent, reflect and restart. Order shall return.
Please Subscribe to my youTube channel.
nick_ceneviva
11 - Bolide

Solution is attached.  Not sure if I took the most efficient way for determining the closest prime number but it works!

 

 

Joe_Mako
12 - Quasar

Can we use R?

 

Spoiler
r prime.png

using the accepted solution at:
https://stackoverflow.com/questions/19767408/prime-number-function-in-r
to check if a number is prime

I made the attached primarlly in R
1. the R code will return the previous and next prime number
# Expectes a field named "Number"
# Adds the fields "Prev_Prime" and "Next_Prime"

# Function to check if a number is prime
is.prime <- function(n){ n == 2L || all(n %% 2L:max(2,floor(sqrt(n))) != 0)}

# Function to get next prime
next.prime <- function(n){
while(! is.prime(n)){n<-n+1}
return(n)}

# Function to get previous prime
prev.prime <- function(n){
while(! is.prime(n)){n<-n-1}
return(n)}

# Read the Table in
the_data<-read.Alteryx("#1", mode="data.frame")

# Compute the previous prime number
the_data<-cbind(the_data,Prev_Prime = mapply(function(n) prev.prime(n),the_data$Number))

# Compute the next prime number
the_data<-cbind(the_data,Next_Prime = mapply(function(n) next.prime(n),the_data$Number))

# Output the data
write.Alteryx(the_data, 1)

2. Formula tool to compute the closest, returning the next when they are equidistant:
IF [Number]-[Prev_Prime]<[Next_Prime]-[Number] THEN [Prev_Prime] ELSE [Next_Prime] ENDIF
NicoleJohnson
ACE Emeritus
ACE Emeritus

My solution attached! Definitely borrowed the concept of testing for closest prime in either direction from @MarqueeCrew (thanks Mark!), but definitely a different path to the final solution. Added one more calculation for which number was technically "closer": Number itself, the next prime, the previous prime, or tied.

 

Spoiler
WeeklyChallenge79.JPG

Happy Day o' Prime Numbers!

 

NJ

estherb47
15 - Aurora
15 - Aurora

My solution. Another fun challenge. Decided that the output should be in a report, and should result in the next lowest and highest primes, as well as the closest.

patrick_digan
17 - Castor
17 - Castor
Spoiler
I used a couple iterative macros to hopefully make it more efficient.  The first (outer) iterative macro is going through the possible solutions. So if you enter 5000, it starts at 5001, then 4999, then 5002, etc. The second (inner) iterative macro is going through all the possible divisors. So it starts at 2 and goes up until it reaches the Square Root of the number. For example, 5001 would get to 3, realize it's not a prime since 5001/3=1667, and then start over again at 4999. Here is the inner macro where I use the modulo function to check if the quotient is an integer.
Capture.PNG
EmanueleE
8 - Asteroid

Here my Solution!!!

 

Into this app you can write your number and it finds the nearest prime number (above or below the chosen number).

 

Spoiler
flow.PNGmacro1.PNG

challenge-79-EE.PNG

 

PhilipMannering
16 - Nebula
16 - Nebula
Spoiler
Solution

1. Check if number is prime (by checking the number doesn't divide cleanly by any number from 2 up to the square root of the number)

2. If not prime -> check number +/- 1, then +/- 2, then +/- 3, etc. Do this by using an iterative macro and using number = number * (-1)^n x n where n is the number of iterations.

The App using an iterative macroCheck if prime, and iterate if not.

lminors
9 - Comet

Ah lovely @JoeM! Spent the last week playing with iterative macros so seemed like an obvious choice when I saw the challenge. Solution below:

Spoiler
Simple App InterfaceSimple App Interface

Each iteration adds and takes the Iteration number from the original number and then runs a simple Primality test on it (looking at the modulus with each possible divisor) - the first number that passes through with only 2 results with a modulus of 0 is passed out the macro and ends the iteration.

Iterative Prime MacroIterative Prime Macro

Thoroughly enjoyable.

 

Luke

Alteryx Certified Partner with Keyrus UK