#Big O Notation Help
1 messages · Page 1 of 1 (latest)
why would you say O(log(n)) and not O(n^2)?
First thing that come to my mind when I skim thorough this is that I see two nested loop (nested loop mean something time something thus n*n thus** n^2** thus O(n^2).
Does not mean your algo is O(n^2), but this is my first thought
I mixed up my other question - sorry. Yes, two loops typically means O(n^2), but what was tripping me up is that i += j in the while loop. that is changing the condition of the loop, so it wouldn’t actually run n times, no?
;compile perl
my $i = 1;
my $j = 1;
my $run = 0;
$n = 16;
while($i <= $n) { $i = $i + 1; }
print "[$i]\n";
for($j = 1; $j <= $n; ++$j)
{
$i = $j;
my $loop = 0;
while($i <= $n)
{
$i = $i + $j;
++$loop;
++$run;
}
#print "Loop[$i][$n]";
#print "[$loop] ";
#print ".";
}
print "\nI[$i] J[$j] N[$n] R[$run]\n ";
exit;
[17]
I[32] J[17] N[16] R[50]
hm...interesting. i don't fully understand perl syntax, but i guess so. so it'd be O(n^2)
so it wouldn't be on^2
16 log10( 16 ) = 19.26
16 log2( 16 ) = 64
O(n log2 n)
16 ln 16 = 44.36
;compile perl
my $i = 1;
my $j = 1;
my $run = 0;
$n = 64;
while($i <= $n) { $i = $i + 1; }
print "[$i]\n";
for($j = 1; $j <= $n; ++$j)
{
$i = $j;
my $loop = 0;
while($i <= $n)
{
$i = $i + $j;
++$loop;
++$run;
}
#print "Loop[$i][$n]";
#print "[$loop] ";
#print ".";
}
print "\nI[$i] J[$j] N[$n] R[$run]\n ";
exit;
[65]
I[128] J[65] N[64] R[280]
2^6 = 64
64*6 = 384
so it's better than O(n log2 n)
n log3 n = 242
n log 2.5 n = 290
O(n log 2.58 (n) ) = 281~
log2.58(64) = 4.3879822754288
16 * 4.3879822754288 = 70.2
so a little bit better than O( n LOG2 n )
;compile perl
use strict;
use warnings;
my $n = 128;
my $i = 1;
my $j = 1;
my $run = 0;
while($i <= $n) { $i = $i + 1; }
print "[$i]\n";
for($j = 1; $j <= $n; ++$j)
{
$i = $j;
my $loop = 0;
while($i <= $n)
{
$i = $i + $j;
++$loop;
++$run;
}
#print "Loop[$i][$n]";
#print "[$loop] ";
#print ".";
}
print "\nI[$i] J[$j] N[$n] R[$run]\n ";
exit;
[129]
I[256] J[129] N[128] R[645]
128 * 5.04 = 645
This free log calculator solves for the unknown portions of a logarithmic expression using base e, 2, 10, or any other desired base.
;compile perl
use strict;
use warnings;
my $n = 128;
my $i = 1;
my $j = 1;
my $run = 0;
my $total = 0;
while($i <= $n) { $i = $i + 1; ++$total; }
print "[$i]\n";
for($j = 1; $j <= $n; ++$j)
{
$i = $j;
my $loop = 0;
while($i <= $n)
{
$i = $i + $j;
++$loop;
++$run;
++$total;
}
#print "Loop[$i][$n]";
#print "[$loop] ";
#print ".";
}
print "\nI[$i] J[$j] N[$n] R[$run] T[$total]\n ";
exit;
[129]
I[256] J[129] N[128] R[645] T[773]
129 = 128/645/773
;compile perl
use strict;
use warnings;
my $n = 4;
my $i = 1;
my $j = 1;
my $run = 0;
my $total = 0;
while($i <= $n) { $i = $i + 1; ++$total; }
print "[$i]\n";
for($j = 1; $j <= $n; ++$j)
{
$i = $j;
my $loop = 0;
while($i <= $n)
{
$i = $i + $j;
++$loop;
++$run;
++$total;
}
#print "Loop[$i][$n]";
#print "[$loop] ";
#print ".";
}
print "\nI[$i] J[$j] N[$n] R[$run] T[$total]\n ";
exit;
[5]
I[8] J[5] N[4] R[8] T[12]
4 = 4/8/12
129 = 128/645/773
;compile perl
use strict;
use warnings;
sub f($)
{
my $n = $_[0];
my $i = 1;
my $j = 1;
my $run = 0;
my $total = 0;
while($i <= $n) { $i = $i + 1; ++$total; }
#print "[$i]\n";
for($j = 1; $j <= $n; ++$j)
{
$i = $j;
my $loop = 0;
while($i <= $n)
{
$i = $i + $j;
++$loop;
++$run;
++$total;
}
#print "Loop[$i][$n]";
#print "[$loop] ";
#print ".";
}
my $log = log($n) / log(2);
print "\nI=$i J=$j N=$n R=$run T=$total L=$log";
}
f(1);
f(2);
f(4);
f(8);
f(16);
f(32);
f(64);
f(128);
f(1024);
exit;
I=2 J=2 N=1 R=1 T=2 L=0
I=4 J=3 N=2 R=3 T=5 L=1
I=8 J=5 N=4 R=8 T=12 L=2
I=16 J=9 N=8 R=20 T=28 L=3
I=32 J=17 N=16 R=50 T=66 L=4
I=64 J=33 N=32 R=119 T=151 L=5
I=128 J=65 N=64 R=280 T=344 L=6
I=256 J=129 N=128 R=645 T=773 L=7
I=2048 J=1025 N=1024 R=
i would classify it as O(n) to be honest, given that the inner loop runs not n times
faced a similar leetcode problem which is why i'm saying this
but lemme do a little research 1 sec
it runs in O(n) for n < 5
then faster than O(n log2.5 n)
;compile perl
use strict;
use warnings;
sub f($)
{
my $n = $_[0];
my $i = 1;
my $j = 1;
my $run = 0;
my $total = 0;
while($i <= $n) { $i = $i + 1; ++$total; }
#print "[$i]\n";
for($j = 1; $j <= $n; ++$j)
{
$i = $j;
my $loop = 0;
while($i <= $n)
{
$i = $i + $j;
++$loop;
++$run;
++$total;
}
#print "Loop[$i][$n]";
#print "[$loop] ";
#print ".";
}
my $log = log($n) / log(2.55);
print "\nI=$i J=$j N=$n R=$run T=$total L=$log";
}
f(1);
f(2);
f(4);
f(8);
f(16);
f(32);
f(64);
f(128);
f(1024);
exit;
I=2 J=2 N=1 R=1 T=2 L=0
I=4 J=3 N=2 R=3 T=5 L=0.740468003292199
I=8 J=5 N=4 R=8 T=12 L=1.4809360065844
I=16 J=9 N=8 R=20 T=28 L=2.2214040098766
I=32 J=17 N=16 R=50 T=66 L=2.9618720131688
I=64 J=33 N=32 R=119 T=151 L=3.702340016461
I=128 J=65 N=64 R=
that's interesting, I think we can say that the best case is O(n)
worst case is O(log(b)n)
but the base you used was 2.5, no?
yes, but you have to multiply the log by N
n=64 takes 151 total loops and 119 inner loops O( n + n log2 n)
not 64 + 4 loops 😂 O( n + log2 n)
;compile perl
use strict;
use warnings;
sub f($)
{
my $n = $_[0];
my $i = 1;
my $j = 1;
my $run = 0;
my $total = 0;
while($i <= $n) { $i = $i + 1; ++$total; }
#print "[$i]\n";
for($j = 1; $j <= $n; ++$j)
{
$i = $j;
my $loop = 0;
while($i <= $n)
{
$i = $i + $j;
++$loop;
++$run;
++$total;
}
#print "Loop[$i][$n]";
#print "[$loop] ";
#print ".";
}
my $log = log($n) / log(2.55);
print "\nI=$i J=$j N=$n R=$run T=$total L=$log";
}
#f(1024);
f(1024*1024*16);
#f(1024*1024*1024);
exit;
I=33554432 J=16777217 N=16777216 R=281689074 T=298466290 L=17.7712320790128