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
| package main | |
| import "fmt" | |
| type I interface { | |
| M() // uint64 | |
| } | |
| type Vertex struct { | |
| X, Y uint64 |
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
| package main | |
| import "fmt" | |
| type I interface { | |
| M() uint64 | |
| } | |
| type Vertex struct { | |
| X, Y uint64 |
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
| 'use strict'; | |
| const objClone = function clone() { | |
| const clonedObj = {}; | |
| Object.entries(this).forEach((el) => { | |
| clonedObj[el[0]] = el[1]; | |
| }); | |
| return clonedObj; | |
| }; |
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 operator | |
| def karatsuba(x,y): | |
| '''Function to multiply 2 numbers. | |
| More efficient manner than the | |
| grade school algorithm. | |
| ''' | |
| stack = [] |
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
| # 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) |
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
| 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 |
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
| 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. |
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
| 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) |
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 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 |
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
| 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: |