Skip to content

Instantly share code, notes, and snippets.

View tabidots's full-sized avatar

Justin Douglas tabidots

  • Da Nang, Vietnam
View GitHub Profile
@tabidots
tabidots / jaro.clj
Created June 23, 2022 08:55
Jaro similarity in Clojure
(defn ordered-matches
[s1 s2]
(let [enumerate (fn [coll] (map-indexed vector coll))
floor (fn [x] (Math/floor x)) ; cljs: (.floor js/Math x)
window-span (-> (max (count s1) (count s2))
(/ 2) floor dec)]
(remove nil?
(for [[i top] (enumerate s1)] ; cannot be put into a single for-comprehension
(first
(for [[j bottom] (enumerate s2)
@tabidots
tabidots / rand-product-subset.clj
Created May 14, 2019 14:19
Random product subset
(defn rand-approx-product-subset
"Generates a randomized subset of a coll of integers whose product has a chance of
approximating `goal`."
[goal coll]
(let [midrange (-> (apply max coll) (- (apply min coll)) (/ 2.0))
tipping-point (/ goal (Math/sqrt midrange))]
(loop [product 1N coll' (shuffle coll) subset []]
(if (> product tipping-point) subset
(let [x (first coll')]
(recur (*' product x) (rest coll') (conj subset x)))))))
@tabidots
tabidots / brent.clj
Last active April 25, 2019 11:30
[Draft of Pollard-Brent in Clojure] Attempt at writing the Pollard-Brent factorization algorithm in a functional style. Not performant though
;; Hard-coded values for c, m, x here to debug the code and compare performance vs. Python implementation
;; Performance is VERY poor (~4 min). This is the fifth rewrite, and the most functional
;; (imperative attempts did not fare much better, and were incredibly ugly).
;; However, with the same seed values, this does perform the algorithm correctly
;; (that is, identically to the fast Python implementation below), finding a factor at some point in the loop
;; between r = 1048576 and r = 2097152.
;; With reference to Brent's paper "An Improved Monte Carlo Factorization Algorithm
;; https://maths-people.anu.edu.au/~brent/pd/rpb051i.pdf
@tabidots
tabidots / factorization.clj
Created April 15, 2019 04:33
[sieve-factorization] Attempt to do prime factorization using JVM array of smallest prime factors
(ns numthy.factorization
(:require [clojure.math.numeric-tower :as tower]
[numthy.helpers :as h]))
(defn spf-sieve
[n]
;; Adapted from https://stackoverflow.com/a/48137788/4210855
(let [^ints sieve (int-array n (range n))
upper-bound (h/isqrt n)]
(loop [p 2] ;; p's are the bases
@tabidots
tabidots / euler437.clj
Last active April 15, 2019 10:12
[euler437]
(defn spf-sieve
[n]
;; Adapted from https://stackoverflow.com/a/48137788/4210855
(let [^ints sieve (int-array n (range n))
upper-bound (h/isqrt n)]
(loop [p 2] ;; p's are the bases
(if (> p upper-bound) sieve
(do
(when (= p (aget sieve p))
(loop [i (* 2 p)] ;; i's are the multiples of p
@tabidots
tabidots / perfect_powers.clj
Last active April 2, 2019 14:46
[perfect-powers] Testing for perfect powers #number-theory
(defn babylonian-root
"High-precision BigDecimal nth-root using the Babylonian algorithm,
with a close initial approximation for ridiculously fast convergence."
[n root]
(let [eps 0.000000000000000000000000000000000000000001M]
(loop [t (bigdec (tower/expt n (/ root)))] ;; rough initial approx
(let [ts (repeat (- root 1) t)
nxt (with-precision 100 (-> (/ n (reduce *' ts))
(+ (reduce +' ts))
(/ root)))]