MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply TodayThe Golomb's self-describing sequence {G(n)} is the only nondecreasing sequence of natural numbers such that n appears exactly G(n) times in the sequence. The values of G(n) for the first few n are

`n = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 …``G(n) 1 2 2 3 3 4 4 4 5 5 5 6 6 6 6 …`

Write a MATLAB script that will give you G(n) when given n.

Efficiency is key here, since some of the values in the test suite will take a while to calculate.

30 correct solutions
41 incorrect solutions

Last solution submitted on Jan 11, 2016

3 Comments

Richard Zapor
on 10 Jan 2013

I prefer g(end+1:end+g(gptr))=gptr; to usage of repmat. My machine to solve 1234567 takes 48msec vs 15.4 sec using repmat. repmat has a performance issue with large column replication. Unfortunately score is code size and not time.

Alfonso Nieto-Castanon
on 10 Jan 2013

totally agree (not to mention the entire 'growing inside a loop' uglyness), cody style is very far from any reasonable coding standard...

James
on 10 Jan 2013

That's very interesting. The time difference on my (presumably much older) version of MATLAB is much less. Your method gives me an average time of about 18.8 sec, while repmat gives me an average time of 19.5 sec.

1 player likes this solution

2 Comments

Freddy
on 30 Jan 2013

May you give a short explanation on this solution?

Khaled Hamed
on 30 Jan 2013

This is the asymptotic expression of nth term based on the golden ratio. See http://en.wikipedia.org/wiki/Golomb_sequence

1 Comment