Weekly Challenge

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

We're actively looking for ideas on how to improve Weekly Challenges and would love to hear what you think!

Submit Feedback
We've recently made an accessibility improvement to the community and therefore posts without any content are no longer allowed. Please use the spoiler feature or add a short message in the message body in order to submit your weekly challenge.

Challenge #79: Find the Closest Prime Number

Alteryx Community Team
Alteryx Community Team

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.


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



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.







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!



Alteryx Partner

Can we use R?


r prime.png

using the accepted solution at:
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}

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

# 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
14 - Magnetar
14 - Magnetar

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.



Happy Day o' Prime Numbers!



14 - Magnetar
14 - Magnetar

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.

16 - Nebula
16 - Nebula
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.
Alteryx Certified 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).





Alteryx Certified Partner
Alteryx Certified Partner

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 macroThe App using an iterative macroCheck if prime, and iterate if not.Check 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:

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.



Alteryx Certified Partner with Keyrus UK