Skip to content

Instantly share code, notes, and snippets.

View OussaZaki's full-sized avatar
🧪
Experimenting

Oussama Zaki OussaZaki

🧪
Experimenting
View GitHub Profile
@OussaZaki
OussaZaki / euler3_2.py
Created May 13, 2019 16:56
Pollard's rho factorisation
#!/bin/python3
def is_prime(n):
if n < 2:
return False
ps = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
def is_spsp(n, a):
d, s = n-1, 0
@OussaZaki
OussaZaki / euler3_1.py
Last active May 13, 2019 16:23
Refined Eratosthenes sieve factorisation
#!/bin/python3
import math
def max_prime_factor(n):
maxPrime = -1
# We keep dividing n by 2 to get rid of all the even composite factors.
while n % 2 == 0:
maxPrime = 2
n >>= 1 # equivalent to n //= 2
const port = 3000
require('http')
.createServer((req, res) => {
console.log('url:', req.url)
res.end('hello smartcoding')
})
.listen(port, (error)=>{
console.log(`server is running on ${port}`)
})
@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>
@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 / 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 / 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 / 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_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_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