Feb:25(Wed)
sum = 0;
for ( i = 0; i < n; i++ )
for ( j = 0; j < 100; j++ )
sum++;
sum = 0;
for ( i = 0; i < n; i++ )
for ( j = 0; j < n; j++ )
sum++;
sum = 0;
for ( i = 0; i < n; i++ )
for ( j = 0; j < n * n; j++ )
sum++;
sum = 0;
for ( i = 0; i < n; i++ )
for ( j = 0; j < i; j++ )
sum++;
sum = 0;
for ( i = 0; i < n; i++ )
for ( j = 0; j < i * i; j++ )
for ( k = 0; k < j; k++ )
sum++;
sum = 0;
for ( i = 1; i < n; i++ )
for ( j = 1; j < i * i; j++ )
if ( j % i == 0 )
for ( k = 0; k < j; k++ )
sum++;
n
as a command-line argument
(rem. in Eclipse use Run...),
and output the final value of sum.
n
as follows:
time java program_name value_for_n
And among the three times reported by time,
the one that is best for our purposes here is
the one labeled "user".
n,
try 1000 times powers of
two:
1000, 2000, 4000, ... —
or actually 1000000 (one million)
times powers of two for (a).
Continue trying values until you get a time
over twenty seconds.
sum.
[Acknowledgement: this exercise is derived from Mark Allen Weiss.]