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.
Announcement | Get certified today - take the Alteryx Designer Core and Advanced exams on-demand now!

Challenge #79: Find the Closest Prime Number

Director, Customer Enablement
Director, Customer Enablement

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!

 

Alteryx Certified Partner
Alteryx Certified Partner

@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 reboot. Order shall return.
Alteryx Certified Partner

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

 

 

Quasar
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

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

Pulsar

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.

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
Alteryx Partner

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

 

Alteryx Certified Partner
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.

Nearest Prime App.pngThe App using an iterative macroIs Prime Test.pngCheck if prime, and iterate if not.

Alteryx Certified Partner

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
Capture.PNGSimple 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.

Capture2.PNGIterative Prime Macro

Thoroughly enjoyable.

 

Luke

Alteryx Certified Partner with Keyrus UK