Skip to content

Instantly share code, notes, and snippets.

View ShenTengTu's full-sized avatar
🇹🇼
working & study

涂紳騰(Shen-Teng Tu) ShenTengTu

🇹🇼
working & study
View GitHub Profile
@ShenTengTu
ShenTengTu / seq_fn.js
Created July 1, 2017 09:34
Execute functions sequentially with array
var jsList = ["inline.js","polifill.js","vendor.js","main.js"];
var seq = jsList.reduceRight(reducefn,donefn);
seq("Start");
function mockExcuteJs(js,fn){
console.log(`Execute ${js}`);
fn("Execute "+js+" done.");
}
@ShenTengTu
ShenTengTu / isPureObject.js
Created July 1, 2017 09:40
Pure Object validation
/*isPureObject(item,strict)
- item(any) : the target to be checked.
- strict(boolean) : if 'true', just original Object can pass the check.
*/
function isPureObject(item, strict) {
if (item !== null) {
if (typeof item === 'object' && !Array.isArray(item)) {
if (strict) {
if (item.__proto__.__proto__ === null) {
return true;
@ShenTengTu
ShenTengTu / perfect-scrollbar.less
Created July 26, 2017 13:20
all scss file in noraesae's perfect-scrollbar(v7.0.1) to one less file
/*=====perfect-scrollbar=====*/
/*--variables---*/
@ps-border-radius: 6px;
@ps-rail-default-opacity: 0;
@ps-rail-container-hover-opacity: 0.6;
@ps-rail-hover-opacity: 0.9;
@ps-bar-bg: transparent;
@ps-bar-container-hover-bg: #aaa;
@ShenTengTu
ShenTengTu / getExcelDateValue.js
Created August 8, 2017 01:43
Excel Date value ( DATEVALUE() ) Calculation in javascript
//Calculate Excel Date value
// Date 1899/12/31 serial number is 1
console.log(getExcelDateValue(new Date(1400,5,8)));//1400/6/8
console.log(getExcelDateValue(new Date(1899,11,31)));//1899/12/31
console.log(getExcelDateValue(new Date()));//now
function getExcelDateValue(dateObj){
var year = dateObj.getFullYear();
var month = dateObj.getMonth();
@ShenTengTu
ShenTengTu / AttackerSuccessProbability.js
Last active December 13, 2017 08:59
Bitcoin Attacker Success Probability,Javascript version. Reference Satoshi Nakamoto, "Bitcoin: A Peer-to-Peer Electronic Cash System", 2008.
/**
* Reference Satoshi Nakamoto, "Bitcoin: A Peer-to-Peer Electronic Cash System", 2008.
* The attacker's potential progress will be a Poisson distribution with expected value.
* @param [Number] q Probability the attacker finds the next block,(double).
* @param [Number] z The attacker will ever catch up from z blocks behind,(int).
**/
function AttackerSuccessProbability(q,z){
//p = probability an honest node finds the next block
//q = probability the attacker finds the next block
let p = 1.0 - q;//Binomial Random Walk
@ShenTengTu
ShenTengTu / gcd.js
Created December 24, 2017 10:28
Get greatest common divisor(最大公因數) by Euclidean algorithm(輾轉相除法)
/*greatest common divisor(最大公因數)*/
function gcd(x,y){
//Euclidean algorithm(輾轉相除法)
if(y)
while((x%=y) && (y%=x)){}
return x+y;
}
@ShenTengTu
ShenTengTu / sieveOfEratosthenes.js
Last active December 27, 2017 07:22
Implement sieve of Eratosthenes using javascript. A simple, ancient algorithm for finding all prime numbers up to any given limit.
function sieve(x) {
let counter = 0;
let seq = Array(x - 1).fill(2).map((e, i) => e + i);
let primes = [];
while (true) {
if(seq.length === 0){
console.log(`main loop count : ${counter}`);
return primes;
}
@ShenTengTu
ShenTengTu / leastPrimeFactor.js
Created December 27, 2017 08:12
Find least Prime Factor.
function leastPrimeFactor(x){
let seq = Array(x+1);
seq[1] = 1;
for(let i = 2;i<=x;i++){
if(!seq[i]){
seq[i] = i;
for(let j=2*i; j<=x;j+=i){
if(!seq[j])
seq[j] = i;
}
@ShenTengTu
ShenTengTu / primeFactorizationBaseOnEratosthenes.js
Created December 27, 2017 10:17
Prime factorization base on sieve of Eratosthenes.
function primeFactorizationBaseOnEratosthenes(x) {
if (x === 0)
return;
let seq = Array(x + 1); //index:0~x
let sqrt = Math.sqrt(x);
seq[1] = 1;
//Base on sieve of Eratosthene.Get store smallest prime factor.
@ShenTengTu
ShenTengTu / pascal.js
Last active September 22, 2018 15:45
Get binomial coefficients of Pascal's triangle.
function pascal(n) {
if (n === 0)
return [1];
let prev = n - 1;
let times = prev;
let leftLength = Math.floor(n / 2) + 1;
let lead = Array(prev).fill(1);
let temp = [1];
let result_l = [1];
let result_r = [1];