This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
import java.util.*; | |
public class FibonacciHuge { | |
public static long getFibonacciHugeNaive(long n, long m) { | |
if (n <= 1) return n; | |
BigInteger previous = BigInteger.ZERO; | |
BigInteger current = BigInteger.ONE; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.jupiter.api.Assertions.assertEquals; | |
import static org.junit.jupiter.api.Assertions.assertTrue; | |
import java.util.concurrent.TimeUnit; | |
import org.junit.jupiter.api.Test; | |
class FibonacciHugeTest { | |
@Test | |
void getPisanoPeriod() { | |
assertEquals(8, FibonacciHuge.getPisanoPeriod(3)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (int x=0; x<n ;x++){ | |
//statement(s)thattakeconstanttime | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (int x = 0; x < n; x+=k) { | |
//statement(s) that take constant time | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (int i=0; i<n; i++){ | |
for (int j=0; j<m; j++){ | |
//Statement(s) that take(s) constant time | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (int i=0; i<n; i++){ | |
for (int j=0; j<i; j++){ | |
//Statement(s) that take(s) constant time | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (int i=0; i<n; i++){ | |
i*=2; | |
for (int j=0; j<i; j++){ | |
// Statement(s) that take(s) constant time | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i = //constant | |
n = //constant | |
k = //constant | |
while (i < n){ | |
i*=k; | |
// Statement(s) that take(s) constant time | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NestedLoop { | |
public static void main(String[] args) { | |
int n = 10; // 1 step --> Note: n can be anything. This is just an example | |
int sum = 0; // 1 step | |
double pie = 3.14; // 1 step | |
for (int var = 0; var < n; var = var + 3) { // n/3 steps | |
System.out.println("Pie: " + pie); // n/3 steps | |
for (int j = 0; j < n; j = j + 2) { // (n/3 * n/2) steps | |
sum++; // (n/3 * n/2) steps |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NestedLoop { | |
public static void main(String[] args) { | |
int n = 10; // O(time complexity of the called function) | |
int sum = 0; //O(1) | |
double pie = 3.14; //O(1) | |
for (int var = n; var >= 1; var = var - 3) { // O(n/3) | |
System.out.println("Pie: " + pie); // O(n/3) |