Skip to content

Instantly share code, notes, and snippets.

View AnasAboreeda's full-sized avatar
📚
learning one thing about everything and learning everything about one thing

Anas Aboreeda AnasAboreeda

📚
learning one thing about everything and learning everything about one thing
View GitHub Profile
@AnasAboreeda
AnasAboreeda / FibonacciHuge.java
Created January 17, 2022 14:37
Fibonacci Number modulo M
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;
@AnasAboreeda
AnasAboreeda / FibonacciHugeTest.java
Last active January 17, 2022 15:08
Testing Fibonacci Number modulo M
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));
for (int x=0; x<n ;x++){
//statement(s)thattakeconstanttime
}
for (int x = 0; x < n; x+=k) {
//statement(s) that take constant time
}
for (int i=0; i<n; i++){
for (int j=0; j<m; j++){
//Statement(s) that take(s) constant time
}
}
for (int i=0; i<n; i++){
for (int j=0; j<i; j++){
//Statement(s) that take(s) constant time
}
}
for (int i=0; i<n; i++){
i*=2;
for (int j=0; j<i; j++){
// Statement(s) that take(s) constant time
}
}
i = //constant
n = //constant
k = //constant
while (i < n){
i*=k;
// Statement(s) that take(s) constant time
}
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
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)