Skip to content

Instantly share code, notes, and snippets.

View OussaZaki's full-sized avatar
🧪
Experimenting

Oussama Zaki OussaZaki

🧪
Experimenting
View GitHub Profile
long long euler1(long long N) {
long long i, sum = 0;
for (i = 3; i < N; i++) {
if (i % 3 == 0 || i % 5 == 0) {
sum += i;
}
}
return sum;
}
def sum(n, k):
d = n // k
return k * (d * (d+1)) // 2
def euler1(n):
return sum(n, 3) + sum(n, 5) - sum(n, 15)
t = int(input().strip())
for i in range(t):
N = int(input().strip())
@OussaZaki
OussaZaki / euler2_1.py
Created June 25, 2017 20:18
Naive implementation of Project Euler #2: Even Fibonacci numbers.
def even_fibonacci_sum(n):
fn_2 = 1 #Fn-2
fn_1 = 1 #Fn-1
sum = 0
while True :
fn = fn_2 + fn_1 #Fn
if fn >= n: return sum
if fn % 2 == 0: sum += fn
fn_2, fn_1 = fn_1, fn
@OussaZaki
OussaZaki / euler2_2.py
Created June 26, 2017 15:09
Better implementation of Project Euler #2: Even Fibonacci numbers.
def even_fibonacci_sum(n):
fn_2 = 2 #Fn-2
fn_1 = 8 #Fn-1
sum = 10 #first even number Fn-2 + Fn-1
while True :
fn = 4 * fn_1 + fn_2
if fn >= n: return sum
sum += fn
fn_2, fn_1 = fn_1, fn
@OussaZaki
OussaZaki / euler2_3.py
Created June 28, 2017 15:41
Explicit expression of the sum of the even Fibonacci numbers
from math import sqrt, floor, log
phi = (1 + sqrt(5)) / 2
psi = (1 - sqrt(5)) / 2
def reverse_fib(fn):
return floor(log((fn * sqrt(5) + sqrt(5 * (fn ** 2) - 4)) / 2, phi))
def get_k(n):
return reverse_fib(n) // 3
@OussaZaki
OussaZaki / euler2_4.py
Created June 28, 2017 16:50
Explicit expression of the sum of the even Fibonacci numbers using the Python Decimal library
from math import sqrt, floor, log
from decimal import *
context = Context(prec=3000, rounding=ROUND_05UP)
setcontext(context)
phi = Decimal(1 + Decimal(5).sqrt()) / Decimal(2)
psi = Decimal(1 - Decimal(5).sqrt()) / Decimal(2)
def reverse_fib(fn):
@OussaZaki
OussaZaki / euler2_5.py
Created June 28, 2017 16:57
Sum of the even Fibonacci numbers using preprocessing.
from bisect import bisect
even_fibs = [2, 8, 34, 144, 610, 2584, 10946, 46368, 196418, 832040, 3524578, 14930352, 63245986, 267914296, 1134903170,
4807526976, 20365011074, 86267571272, 365435296162, 1548008755920, 6557470319842, 27777890035288,
117669030460994, 498454011879264, 2111485077978050, 8944394323791464, 37889062373143906]
t = int(input().strip())
for i in range(t):
N = int(input().strip())
print(sum(even_fibs[:bisect(even_fibs, N)]))
@OussaZaki
OussaZaki / Interview1_1.py
Created July 12, 2017 10:37
Average of Levels in Binary Tree using BFS
def averageOfLevels(self, root):
# In case the tree is empty (you never know :P)
if root is None:
return
# Create the queue for BFS and the list of averages
queue = []
averages = []
# Enqueue Root and initialize nodes_count_per_level and sum_per_level
@OussaZaki
OussaZaki / Alert.jsx
Last active February 22, 2018 14:38
Conditional Rendering - Alert Component
function Alert(props) {
if (!props.showAlert) {
return null;
}
return (
<div className={props.type}>
<h4>{props.title}</h4>
<p>{props.summary}</p>
</div>
@OussaZaki
OussaZaki / Loader.jsx
Last active February 22, 2018 14:42
Conditional Rendering - Loader Component
function Loader(props) {
if (!props.showLoader) {
return null;
}
return (
<div className="loader">
<WavyLoader black />
<p>{props.message}</p>
</div>