Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

#Day95#100DaysChallenge- Matlab Loops |Golomb Sequence

#Day95-Golomb Sequence 

Task:

Write a code to print Golomb Sequence 
g =

  1     2     2     3     3     4     4     4     5     5     5     6

Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.

Matlab code

function g=golombseries(n)

%%same as euler341

a = zeros(1,n);

a(1,1) = 1;

for j = 2:n

a(1,j) = 1+a(1,j-a(1,a(1,j-1))); 

 

end

g=a;

end

 Sample Input and Output

>> g=golombseries(7)


g =


     1     2     2     3     3     4     4


>> 12


ans =


    12


>> g=golombseries(12)


g =


     1     2     2     3     3     4     4     4     5     5     5     6


Click here for Video description



No comments

Popular Posts