Addition-chain exponentiation: Difference between revisions

Content added Content deleted
(Go solution)
m (<lang> needs a language)
Line 156: Line 156:
printf("%14d %7d %7d\n", i, seq_len(i), bin_len(i));
printf("%14d %7d %7d\n", i, seq_len(i), bin_len(i));
return 0;
return 0;
}</lang>output<lang>...
}</lang>
output
<pre>...
Exponents:
Exponents:
1 2 4 8 10 18 28 46 92 184 212 424 848 1696 3392 6784 13568 27136 27182
1 2 4 8 10 18 28 46 92 184 212 424 848 1696 3392 6784 13568 27136 27182
Line 177: Line 179:
92 8 9
92 8 9
93 9 10
93 9 10
...</lang>
...</pre>



=={{header|Go}}==
=={{header|Go}}==
Line 438: Line 439:
count of multiplies: 18
count of multiplies: 18
</pre>
</pre>

=={{header|MATLAB}} / {{header|Octave}}==
=={{header|MATLAB}} / {{header|Octave}}==
I assume that Matlab and Octave have about such optimization already included. On a single core of an "Pentium(R) Dual-Core CPU E5200 @ 2.50GHz", the computation of 100000 repetitions of the matrix exponentiation A^27182 and A^31415 take about 2 and 2.2 seconds, resp.

I assume that Matlab and Octave have about such optimization already included. On a single core of an "Pentium(R) Dual-Core CPU E5200 @ 2.50GHz", the computation of 100000 repetitions of the matrix exponentiation A^27182 and A^31415 take about 2 and 2.2 seconds, resp.

<lang Matlab>x = sqrt(.5);
<lang Matlab>x = sqrt(.5);
A = [x,0,x,0,0,0;0,x,0,x,0,0; 0,x,0,-x,0,0;-x,0,x,0,0,0;0,0,0,0,0,1; 0,0,0,0,1,0];A
A = [x,0,x,0,0,0;0,x,0,x,0,0; 0,x,0,-x,0,0;-x,0,x,0,0,0;0,0,0,0,0,1; 0,0,0,0,1,0];A
t = cputime(); for k=1:100000,x1=A^27182;end; cputime()-t,x1,
t = cputime(); for k=1:100000,x1=A^27182;end; cputime()-t,x1,
t = cputime(); for k=1:100000,x2=A^31415;end; cputime()-t,x2, </lang>
t = cputime(); for k=1:100000,x2=A^31415;end; cputime()-t,x2, </lang>

Output:
Output:
<pre>Octave3.4.3> t = cputime(); for k=1:100000,x1=A^27182;end; cputime()-t,x1,
<pre>Octave3.4.3> t = cputime(); for k=1:100000,x1=A^27182;end; cputime()-t,x1,