BreakPoint

cancel
Showing results for 
Search instead for 
Did you mean: 

Fun Coding Challenges / Questions

As I go through the forums or even work on my own projects I occasionally run across something that, after writing it up, I think "That was fun!" or "That was harder than I thought it would be".

 

So, let's post interesting questions or problems that you come across and let others see what answers they come up with.

Message 1 of 53
(9,522 Views)

OK, so I meant to get this started earlier but got busy.  I'll also admit that I shamelessly stole the idea from reddit, and so I am going to shamelessly steal the first puzzle.  I don't expect later submissions to be this well written or extensive, but this one looked fun.

 


 

 

This exercise is inspired by a Numberphile video (no need to watch past 2:00).

Description

A number of sailors (let's call it N) are stranded on an island with a huge pile of coconuts and a monkey. During the night, each sailor (in turn) does the following without the others knowing:

  1. He takes one N'th (e.g. if N=5, one fifth) of the coconuts in the pile and hides them
  2. The division leaves one coconut left over, which is given to the monkey.

In the morning, they split the remaining coconuts between them. This time the split is even. There's nothing left over for the monkey.

Your task: Given the number of sailors (N), how many coconuts were in the pile to begin with (lowest possible number)?

Formal inputs/outputs Input

The input is a single number: N, the number of sailors. This number is a whole number that is greater than or equal to 2.

Output

The output is a single number: the number of coconuts in the original pile.

Sample input/output

Input:

5

Output:

3121

Sample solution for 5 sailors: https://jsfiddle.net/722gjnze/8/

Credit

This challenge was originally suggested on /r/dailyprogrammer_ideas by /u/TinyLebowski (prior to some changes by me). Have a cool challenge idea? Hop on over to /r/dailyprogrammer_ideas to tell everyone about it!


 

Edit: Source: https://www.reddit.com/r/dailyprogrammer/comments/43ouxy/20160201_challenge_252_easy_sailors_and_mon...

Message 2 of 53
(9,498 Views)

It's not an individual challenge, but I've fount the https://projecteuler.net/ website, and I've been attempting to go through all the puzzles here and attempt to solve in LabVIEW (only). They can get tough very quickly.

Certified-LabVIEW-Architect_rgb.jpgCertified_TestStand_Architect_rgb.jpg


"I won't be wronged. I won't be insulted. I won't be laid a-hand on. I don't do these things to other people, and I require the same from them." John Bernard Books

Message 3 of 53
(9,489 Views)

That was fun, I needed a break this afternoon. Once you get to 8 sailors it takes a while. 🙂

 

Spoiler
sailors2.png
Message 4 of 53
(9,486 Views)

Funny you start his thread today because I was thinking about irational numbers.

 

Spoiler
I have a seven year old G-daughter that I have been sneaking in math lesson when she is not looking.

 

 

Is it even possible to contrive a test of irrationality in a computer environment where everything has to be entered as a rational number?

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 5 of 53
(9,474 Views)

Here is my attempt.

 

Spoiler
Monkey.png
0 Kudos
Message 6 of 53
(9,452 Views)

@Ben wrote:

Funny you start his thread today because I was thinking about irational numbers. [...]

 

Ben


Are irrational numbers the numbers engineers get from managers?  Budgets, deadlines...

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 7 of 53
(9,397 Views)

I have a VI (it's actually two VIs, is that breaking some rules?) that computes the answer for 9 sailors in 10.8ms.  Answer for 8 sailors takes 4.5ms.

 

I'll post them on Monday.

 

Smiley Tongue

0 Kudos
Message 8 of 53
(9,359 Views)

Hmm, removing a recursive VI call and replacing it with a for loop gets my timing for the 8 sailor solution down to 0.4ms.....  Funnily enough the 9 sailor solution only takes 1.4ms.  Seems like the Recursive VI call overhead was pretty significant in the previous example.  I'll tidy up the code and post both versions next week.  I think I'll implement an I64 version to get to higher numbers.

0 Kudos
Message 9 of 53
(9,336 Views)

@Intaris wrote:

I have a VI (it's actually two VIs, is that breaking some rules?) that computes the answer for 9 sailors in 10.8ms.  Answer for 8 sailors takes 4.5ms.

 


Hmm, noticed some patterns and do 8 in under 2us. Have to work out the theory.... 😉

0 Kudos
Message 10 of 53
(9,328 Views)