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.
Getting started with Designer? | Start your journey with our new Learning Path!

Challenge #79: Find the Closest Prime Number


Good challenge!


Mine slows down as the numbers entered gets higher, but works. Probably a more elegant solution that I'll look into now.

Alteryx Certified Partner

Oooooh, I liked this one. Iterative macros ahoy!


Ok, so I started with creating the maths behind a prime number identifier. I used a generate rows tool using RowCount <= sqrt([Number]) as the expression, beginning at 1 and looping by +1. Effectively this creates every factor between 1 and the square root of the number.
In the example of 100, this creates rows between 1 and 10.
Dividing 100 by each of these numbers and you are presented with decimal numbers and/or factors. I then split the resulting column by the decimal point and counted the instances of a null in the second column. This counts the numbers of results without a decimal place, ie a full number. this represents a number by which the original number is divisible.
Once I had determined if the number was prime, I changed the workflow to be an iterative macro, built in an iterative formula on the number and placed a filter on the field that determines if the number is prime. On the true output I placed a macro output which represents the end of the iteration, that is when the answer is Y, and on the false output I joined by to the input to form the iterative loop.
This is my iterative macro, "Prime". I then copied this, replaced the iterative + with iterative - to run down from the number and saved this as "Prime2": Macro.PNG

Iterative Macro.PNG

I then built an analytic app to allow the user to select the number and an HTML output to view the results. 
This will show the closest, next and previous prime number for any number selected (up to 100,000,000).




My favourite one so far! A true head scratcher ;)


Used my knowledge of mathematics & macros to get this one! 

Alteryx Certified Partner

Duplicate post

Alteryx Certified Partner
I ended up with two slightly different macros running in parallel to get the higher/lower prime on either side of the input number n.


This was a good recap of iterative macros. I enjoyed this challenge so much I extended it and then wrote a blog post about it! 


Nice Challenge.  My first iterative macro.  Tricksy beasts.  It's the first time where I've found that disabling Unknown field pass through is actually useful.


Tip 'o the hat to @MarqueeCrew for the idea of bracketing the starting number and searching from there



Solution consists of the app and 2 nested Macros.  The innermost one (IsPrime) checks to see if a number is prime using the sieve approach.  Two optimizations here
1.  if the number is even, it by passes the sieve completely
2.  the sieve itself only needs odd divisors since the number is guaranteed to be odd by #1


The outer macro(ClosestPrimeFinder) iterates through the numbers larger and small than the input, calling IsPrime until a prime is found


The App checks if the input is prime first and if not passes the input number to ClosestPrimeFinder.  

Solution 79.png





One of my favorite challenges!


Challenge #79.PNG
Challenge #79 macro2.PNGChallenge #79 macro1.PNG


Alteryx Partner


That was quite fun


App with nested macros
main app.pngMain App - tests whether the number provided itself is prime or not, and calls iterative macro to find closest primeclosestPrime.pngIncrease/Decrease values to get closestisprime.pngis odd / eveniterative.pngis the number divisible by any odd number < sqrt?