Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

Weekly Challenges

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

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

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

SUBMIT YOUR IDEA

Challenge #79: Find the Closest Prime Number

SeanAdams
17 - Castor
17 - Castor

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.

2017-10-08_21-16-33.png

Result:
2017-10-08_21-14-52.png
Iterative Macro
Part which finds the primes

2017-10-08_21-15-27.png

 

 

LordNeilLord
15 - Aurora

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
Weekly Challenge 79 Macro.pngWeekly Challenge 79.png
LandonG
8 - Asteroid

Solution attached.

PhilipMannering
16 - Nebula
16 - Nebula

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

ggruccio
ACE Emeritus
ACE Emeritus

Solved using mod and nested macros

samN
10 - 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. 

jamielaird
14 - Magnetar

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
Screen Shot 2017-12-29 at 10.56.54.png
jasperlch
12 - Quasar

Solution attached.

Natasha
9 - Comet

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.

Screen Shot 2018-01-07 at 20.24.07.pngScreen Shot 2018-01-07 at 20.24.51.png

 

 

 

 

JosephSerpis
17 - Castor
17 - Castor

Challenge Completed