Skip to content

Instantly share code, notes, and snippets.

View codecakes's full-sized avatar
💭
I may be slow to respond.

codecakes codecakes

💭
I may be slow to respond.
View GitHub Profile
@codecakes
codecakes / no_return_interface.go
Created August 25, 2018 14:12
if you are not returning, the implementation method type is not needed
package main
import "fmt"
type I interface {
M() // uint64
}
type Vertex struct {
X, Y uint64
@codecakes
codecakes / return_interface.go
Created August 25, 2018 14:13
if you are returning, an implementation type is needed
package main
import "fmt"
type I interface {
M() uint64
}
type Vertex struct {
X, Y uint64
@codecakes
codecakes / objectClonePrototype.js
Created May 14, 2019 07:43
Create an inheritance for a newly created var from its Type's Prototype
'use strict';
const objClone = function clone() {
const clonedObj = {};
Object.entries(this).forEach((el) => {
clonedObj[el[0]] = el[1];
});
return clonedObj;
};
@codecakes
codecakes / karatsuba_fast_exponentiation.py
Last active July 8, 2019 08:28
karatsuba using fast exponentiation i.e. Factors number in powers of 2 using its binary form that sum up to it
import operator
def karatsuba(x,y):
'''Function to multiply 2 numbers.
More efficient manner than the
grade school algorithm.
'''
stack = []
@codecakes
codecakes / digits_factorial.py
Created September 15, 2019 23:03
digits in a factorial number result
# See: https://mathoverflow.net/questions/19170/how-good-is-kamenetskys-formula-for-the-number-of-digits-in-n-factorial/44927#44927
# See: https://math.stackexchange.com/questions/661227/factorial-length
import math
import functools
_LIMIT = 5*pow(10, 7)
def digitsInFactorial(N: int) -> int:
cache_ln = functools.lru_cache(maxsize=None)(math.log)
@codecakes
codecakes / latency.txt
Created September 17, 2019 21:07 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@codecakes
codecakes / euler_tot.py
Created September 25, 2019 10:20
Eulers totient function
from __futures__ import division
import functools
def phi(n):
'''Eulers totient function.
Thanks https://cp-algorithms.com/algebra/phi-function.html
params:
n: int, a whole number.
@codecakes
codecakes / aks.py
Created September 25, 2019 11:28
aks primality test
def aks_prime(n):
# 1
lgN = math.log2(n)
for b in range(2, lgN+1):
a = pow(n, 1/b)
if instance(a, int):
return False
#2
max_k = lgN**2
max_r = max(3, lgN**5)
import math
def isPrime(n):
limit = n + 1
primes = set()
not_primes = set()
primes.add(2)
for num in range(3, limit):
# if odd and not in primes yet
@codecakes
codecakes / isPrime.py
Created September 30, 2019 15:34
is Prime
def isPrime(n):
if n < 2: return False
if n >2 and not n%2:
return False
limit = n + 1
primes = set()
not_primes = set()
for num in range(3, limit):
# if odd and not in primes yet
if num % 2 and num not in not_primes: