cancel
Showing results 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

Nebula

any day that involves macros is a good day.

My solution was very similar to @PhilipMannering @EmanueleE @lminors

Spoiler

Used an iterative macro to iterate with progressively bigger (or smaller) numbers until we find a prime.    Then there's a simple container that checks for the bigger or smaller version.

Container
runs the iterative macro to find the first prime >= input number; and the first prime <= input number.

Result:

Iterative Macro
Part which finds the primes

Alteryx Certified Partner

Maths is not my strong point so I went for the brute force approach and check a wide range of numbers until the closest prime was found!

Spoiler
Asteroid

Solution attached.

Alteryx Certified Partner

Solution wasn't properly exported as a packaged workflow. Here it is attached :)

Bolide

Solved using mod and nested macros

Fireball

Hm i am somewhat happy with my solution. Does anyone know if i am equating an iterative macro to a for/while-loop, is there an equivalent to a break?

Spoiler
My innermost macro makes up for my outer one.
The inner most macro loops from 3-squareroot of the the number to see if mod(n)=0 and increments by two.
The outer macro makes iterations away from the given number to try and fine the closest prime.
My solution currently finds more than just the closest as my filtering is not the most sophisticated, hence my question about breaks in iterative  macros

Runs fast enough that despite its shortcomings im not too disappointed.

Alteryx Certified Partner

I wasn't sure where to start with this one, and since I've never had a chance to use the R tool I thought I'd attempt to replicate @JoeM's.

I used the same code from Stack Overflow except with the prime function written out as an IF THEN ELSE which was more intuitive to me. Probably less efficient but definitely easier to read.

I'll definitely be exploring the R tool more. I think I've just had my eyes opened to how many possibilities this opens up! Can't wait for a similar Python tool.

Spoiler
Quasar

Solution attached.

Alteryx Certified Partner

I half-solved this challenge a while ago, had only to update it slightly today.

Spoiler

Initially I build a workflow that checks if the number is prime or not so now I had only update it to be an iterative macro. In fact I built two macro, one that adds one to the initial entered number and the other that subtract from it.

The macro finds a square root of the entered number and then generate rows for all odd factors. Then I check the mod of a number, and transform all that equal zero to nulls. Then in summarise tool I find the the count of null mods, and if it is 0 then this is a prime number. A bit of a brute-force approach, I guess it could be a bit optimised by checking at the start if the number is even or not, so we can skip the whole stage of generating rows.

Alteryx Certified Partner

Challenge Completed