Skip to content

Commit 7bc4bb6

Browse files
author
Wakidur Rahaman
committed
big o
1 parent 646c812 commit 7bc4bb6

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/big-O-complexities/big-O-complexities-01.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
* O(n2) is quadratic time,
1313
*
1414
* O(n3) is cubic time.
15+
*
16+
* logarithmic time complexity
17+
* is printing elements that are a power of 2 between 2 and n
1518
1619
1720
*/
@@ -45,3 +48,13 @@ function printCubicTime(array) {
4548
}
4649
var cubicTimeArray = ['a', 'b', 'c', 'd', 'e', 'f'];
4750
console.log(printCubicTime(cubicTimeArray));
51+
/**
52+
* The efficiency of logarithmic time complexities is apparent with large inputs such as a million items.
53+
* Although n is a million, exampleLogarithmic will print only 19 items because log2(1,000,000) = 19.9315686.
54+
*/
55+
function printLogarithmic(n) {
56+
for (var i = 2; i <= n; i = i * 2) {
57+
console.log(i);
58+
}
59+
}
60+
printLogarithmic(1000000);

src/big-O-complexities/big-O-complexities-01.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
* O(n2) is quadratic time,
1313
*
1414
* O(n3) is cubic time.
15+
*
16+
* logarithmic time complexity
17+
* is printing elements that are a power of 2 between 2 and n
1518
1619
1720
*/
@@ -52,3 +55,16 @@ function printCubicTime(array: string[]) {
5255
}
5356
const cubicTimeArray = ['a', 'b', 'c', 'd', 'e', 'f'];
5457
console.log(printCubicTime(cubicTimeArray));
58+
59+
/**
60+
* The efficiency of logarithmic time complexities is apparent with large inputs such as a million items.
61+
* Although n is a million, exampleLogarithmic will print only 19 items because log2(1,000,000) = 19.9315686.
62+
*/
63+
64+
function printLogarithmic(n: number) {
65+
for (let i = 2; i <= n; i = i * 2) {
66+
console.log(i);
67+
}
68+
}
69+
70+
printLogarithmic(1000000);

0 commit comments

Comments
 (0)