Jump to content

Fibonacci word: Difference between revisions

Added iterative approach →‎{{header|Scilab}}
(Added iterative approach →‎{{header|Scilab}})
Line 3,300:
 
=={{header|Scilab}}==
 
Two different approaches were implemented, and their execution times can be compared. Both examples use Scilab's [[Entropy#Scilab|entropy]] example.
 
===Recursive approach===
It uses Scilab's [[Entropy#Scilab|entropy]] example.
<lang>exec('.\entropy.sci',0);
 
Line 3,318 ⟶ 3,320:
endfunction
 
Nfinal_length = 37;
 
char_length = zeros(N,1);
N=[1:final_length]';
entropies = zeros(N,1);
char_length = zeros(N);
entropies = zeros(N);
tic();
for i=1:Nfinal_length
word = fiboword(i);
char_length(i) = length(word);
Line 3,330 ⟶ 3,334:
 
disp('EXECUTION TIME: '+string(time)+'s.');
disp(['N', 'LENGTH', 'ENTROPY'; string([[1:N]' char_length entropies])]);</lang>
 
{{out}}
<pre> EXECUTION TIME: 442.87612s.
 
!N LENGTH ENTROPY !
! !
!1 1 0 !
! !
!2 1 0 !
! !
!3 2 1 !
! !
!4 3 0.9182958 !
! !
!5 5 0.9709506 !
! !
!6 8 0.954434 !
! !
!7 13 0.9612366 !
! !
!8 21 0.9587119 !
! !
!9 34 0.9596869 !
! !
!10 55 0.9593160 !
! !
!11 89 0.9594579 !
! !
!12 144 0.9594038 !
! !
!13 233 0.9594244 !
! !
!14 377 0.9594165 !
! !
!15 610 0.9594196 !
! !
!16 987 0.9594184 !
! !
!17 1597 0.9594188 !
! !
!18 2584 0.9594187 !
! !
!19 4181 0.9594187 !
! !
!20 6765 0.9594187 !
! !
!21 10946 0.9594187 !
! !
!22 17711 0.9594187 !
! !
!23 28657 0.9594187 !
! !
!24 46368 0.9594187 !
! !
!25 75025 0.9594187 !
! !
!26 121393 0.9594187 !
! !
!27 196418 0.9594187 !
! !
!28 317811 0.9594187 !
! !
!29 514229 0.9594187 !
! !
!30 832040 0.9594187 !
! !
!31 1346269 0.9594187 !
! !
!32 2178309 0.9594187 !
! !
!33 3524578 0.9594187 !
! !
!34 5702887 0.9594187 !
! !
!35 9227465 0.9594187 !
! !
!36 14930352 0.9594187 !
! !
!37 24157817 0.9594187 !</pre>
 
===Iterative approach===
<lang>exec('.\entropy.sci',0);
 
final_length = 37;
 
word_n = '';
word_n_1 = '';
word_n_2 = '';
 
N = [1:final_length]';
word_length = zeros(N);
entropies = zeros(N);
 
tic();
for i = 1:final_length
if i == 1 then
word_n = '1';
elseif i == 2
word_n = '0';
elseif i == 3
word_n = '10';
word_n_1 = '1';
else
word_n_2 = word_n_1;
word_n_1 = word_n;
word_n = word_n_2 + word_n_1;
end
word_length(i) = length(word_n);
entropies(i) = entropy(word_n);
end
time = toc();
 
disp('EXECUTION TIME: '+string(time)+'s.');
disp(['N', 'LENGTH', 'ENTROPY'; string([N word_length entropies])]);</lang>
 
{{out}}
 
<pre> EXECUTION TIME: 39.652529s.
 
!N LENGTH ENTROPY !
Cookies help us deliver our services. By using our services, you agree to our use of cookies.