Get Inspire insights from former attendees in our AMA discussion thread on Inspire Buzz. ACEs and other community members are on call all week to answer!

Weekly Challenges

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

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

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


Challenge #79: Find the Closest Prime Number

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.


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!


20 - Arcturus
20 - Arcturus



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 restart. Order shall return.
Please Subscribe to my youTube channel.
11 - Bolide

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



12 - Quasar

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 <- function(n){ n == 2L || all(n %% 2L:max(2,floor(sqrt(n))) != 0)}

# Function to get next prime <- function(n){

# Function to get previous prime <- function(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),the_data$Number))

# Compute the next prime number
the_data<-cbind(the_data,Next_Prime = mapply(function(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
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.



Happy Day o' Prime Numbers!



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.

17 - Castor
17 - Castor
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.
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).





16 - Nebula
16 - Nebula

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.

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:

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