Skip to content

Instantly share code, notes, and snippets.

View joshuashaffer's full-sized avatar
🎯
练习关系

. joshuashaffer

🎯
练习关系
  • Shijiazhuang Donghua Jinlong Chemical Co., Ltd.
  • Shijiazhuang, Hebei, China
  • X @BertChintez
View GitHub Profile
@joshuashaffer
joshuashaffer / cauchyEstimator.m
Created October 15, 2017 15:12
Estimate Parameters of Cauchy Distribution.
function q = cauchyEstimator(x)
N = numel(x);
A = @(t) sum(1 ./ (t(1) - x + t(2) .^ 2) .^ 2);
B = @(t) 2 * t(2) * sum(1 ./ (t(1) - x + t(2) .^ 2) .^ 2);
D = @(t) -(N - 2 * sum(1 ./ (t(1) - x + (t(2) ^ 2)) .^ 2 .* ((t(2) .^ 2) - t(1) + x)) * t(2) .^ 2) ./ t(2) .^ 2;
J = @(t) [A(t) B(t); B(t) D(t)];
F = @(t)[-sum((2 * t(1) - 2 .* x) ./ ((t(1) - x) .* (t(1) - x) + t(2)*t(2))),N/t(2)-sum(2*t(2)./((t(1)-x).^2+t(2)^2))];
@joshuashaffer
joshuashaffer / numberOfTriangles2.py
Created October 15, 2017 15:14
Calculate Number of Triangles Possible from array of lengths.
from itertools import combinations as C
def numberOfTriangles2(s):
s = sorted(s)
return sum(sum(1 for _ in filter(lambda r: s[x-1] < sum(r),C(s[:x-1],2))) for x in range(3,len(s)+1))
@joshuashaffer
joshuashaffer / fibFib.f90
Created October 15, 2017 15:16
Is a number in the sequence of fibonacci numbers squared?
function fibFib(f) result(r)
implicit real(8) (t-z)
integer :: f,n
logical :: r
n = 0
r = .false.
do
v = sqrt(5D0) / 2
w = (3 / 2D0 - v) ** n
@joshuashaffer
joshuashaffer / isTriangle.cpp
Created October 15, 2017 15:18
Is a number a triangle number.
bool isTriangle(auto p) {
return fmod((1+8*p)/2,1)<.1;
}
@joshuashaffer
joshuashaffer / fibAt.py
Created October 15, 2017 15:19
Find the nth fibonacci number, and return it raised to the power of n. As the result can be very large, return it as a string.
r = math.sqrt(5)
def fibAt(n):
B = (1-r)/2;
A = (1+r)/2;
return "{}".format(int(.5+(A ** n + B ** n)/r) ** n if n > 1 else 1)
@joshuashaffer
joshuashaffer / wallisFormula.m
Created October 15, 2017 15:23
Integrate the Wallis Formula between [0,pi/2] to the nth power.
wallisFormula = @(n) sqrt(pi)*gamma((n+1)/2)/(n*gamma(n/2));
@joshuashaffer
joshuashaffer / isTournament.f90
Created October 15, 2017 15:25
Is the graph a tournament?
function isTournament(n, A, B) result(o)
implicit integer (a-z)
type(ArrayInteger) :: A, B
logical :: o
o = .false.
if(n<2) return
s = size(A%arr)
do i=1,s
if (A%arr(i)==B%arr(i)) return
@joshuashaffer
joshuashaffer / monkeyBars.cpp
Created October 15, 2017 15:27
Three-Fibonacci sequence.
#define A(z) r[(3+z)&3]
auto monkeyBars(auto n) {
int64_t r[4] = {0,0,1},i=0;
while(i<n) A(i++) = A(i-1) + A(i-2) +A(i-3);
return A(n-1);
}
@joshuashaffer
joshuashaffer / findPathInc.cpp
Created October 15, 2017 15:30
Find Increasing Path in Matrix.
/*You are given an n × m matrix, which contains all the integers from 1 to n * m, inclusive, each exactly once.
Initially you are standing in the cell containing the number 1. On each turn you are allowed to move to an adjacent cell, i.e. to a cell that shares a common side with the one you are standing on now. It is prohibited to visit any cell more than once.
Check if it is possible to visit all the cells of the matrix in the order of increasing numbers in the cells, i.e. get to the cell with the number 2 on the first turn, then move to 3, etc.*/
bool findPath(auto& m, int p = 1, int i=0, int j=0) {
int x = m.size(),y = m[0].size(),k;
if(p==x*y) return 1;
if(p==1)
for(i = 0; i < x;++i)
@joshuashaffer
joshuashaffer / HYPERLOOP.f90
Created October 15, 2017 15:37
DIJKSTRA's algorithm. Graph is inputted as array of string to, array of string from, and int weights.
FUNCTION HYPERLOOP(C1, C2, TU)
IMPLICIT INTEGER (A-Z)
CHARACTER(:), ALLOCATABLE :: C1, C2
TYPE(ARRAYARRAYSTRING) :: TU
TYPE(STRING), DIMENSION(99) :: NA
INTEGER, DIMENSION(:,:), ALLOCATABLE :: AD, AT
INTEGER, DIMENSION(:), ALLOCATABLE :: DI, Q
! CONSTRUCT ID TO NAME MAPPING...
J = 0