Challenge #79: Find the Closest Prime Number
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
any day that involves macros is a good day.
My solution was very similar to @PhilipMannering @EmanueleE @lminors
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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?
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I half-solved this challenge a while ago, had only to update it slightly today.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator