Skip to content

Instantly share code, notes, and snippets.

View jukworks's full-sized avatar

Jonguk Kim jukworks

View GitHub Profile
@jukworks
jukworks / PascalTriangle.java
Created November 2, 2012 06:07
Pascal Triangle in Java
public class PascalTriangle {
public static final int LINES = 10;
public static void main(String[] args) {
int[][] a = new int[LINES][];
for (int i = 0, j = 1; i < LINES; i++, j++) {
a[i] = new int[j];
for (int k = 0; k < j; k++) {
a[i][k] = (i == 0 || k == 0 || k == j - 1) ? 1 : a[i - 1][k - 1] + a[i - 1][k];
}
@jukworks
jukworks / gist:4037243
Created November 8, 2012 06:34
A Sample Code: Getting a Real FFT of 1D array using jTransform library.
import edu.emory.mathcs.jtransforms.fft.DoubleFFT_1D;
public class FftTest {
public static void main(String[] args) {
double[] input = new double[]{
0.0176,
-0.0620,
0.2467,
0.4599,
-0.0582,
@jukworks
jukworks / knight-random-walk.clj
Last active December 24, 2015 03:59
A simulation code for the "A knight's random walk" problem at http://goo.gl/4aXgH
;; possible moves of a knight
(def possible-moves [[2 1] [2 -1] [1 2] [1 -2] [-1 -2] [-1 2] [-2 -1] [-2 1]])
;; he doesn't want to fall down
(defn valid-pos [xs]
(let [[x y] xs]
(and (<= 0 x 7) (<= 0 y 7))))
;; he makes one random move
(defn one-step [xy]
@jukworks
jukworks / rot13.clj
Created October 4, 2013 12:44
ROT-13 encryption in Clojure
(defn shift13 [start n]
(let [s (int start)
c (int n)]
(char (if (<= s c (+ s 25))
(+ s (mod (+ (- c s) 13) 26))
n))))
(defn rot13 [st]
(->> (map (fn [x]
(let [n (int x)]
@jukworks
jukworks / shared-secret.clj
Created October 8, 2013 09:50
A sample code for "Shamir's secret sharing" (http://goo.gl/7PjHF) with the Lagrange interpolation. Written in Clojure.
;; to represent a polynomial, a vector [a b c d] means a + bx + cx^2 + dx^3
;; gen-poly makes a polynomial with n random coefficients (max value: max-coeff)
(defn gen-poly [n max-coeff]
(vec (repeatedly n #(rand-int max-coeff))))
;; if p is negative, returns n
;; else returns n mod p
(defn modp [n p]
(if (<= p 0)
n
@jukworks
jukworks / euclid-gcd.clj
Created October 15, 2013 13:09
Euclid GCD(greatest common divisor) in Clojure
(defn euclid-gcd [m n]
(let [r (mod m n)]
(if (zero? r)
n
(euclid-gcd n r))))
;; (euclid-gcd 2166 6099) => 57
(defn extended-euclid [m n]
(loop [ap 1
a 0
bp 0
b 1
c m
d n]
(let [q (int (/ c d))
r (mod c d)]
(if (zero? r)
@jukworks
jukworks / fetch_all_files.pl
Created February 11, 2014 11:54
This Perl script fetches image and video files from the given URL.
use strict;
use autodie;
use WWW::Mechanize;
use Getopt::Long;
use URI;
use Math::Random qw/random_exponential/;
my $url = '';
my $interval = 5;
my $save = 'fetched';