Skip to content

Instantly share code, notes, and snippets.

View yao2030's full-sized avatar

yao2030 yao2030

  • Shanghai, China
View GitHub Profile
(define (expmod base exp m)
(cond ((= exp 0) 1)
((even? exp)
(remainder (square (expmod base (/ exp 2) m)) m))
(else (remainder (* base (expmod base (- exp 1) m)) m))))
(define (fermat-every n)
(fermat-helper (- n 1) n))
(define (fermat-helper a n)
(define (smallest-divisor n)
(find-divisor n 2))
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (next test-divisor)))))
(define (divides? a b)
(= 0 (remainder b a)))
public class Binary
{
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
int v = 1;
while (v <= N/2)
v = v*2;
int n = N;
while (v > 0)
@yao2030
yao2030 / keep.scm
Created November 23, 2012 03:01
keep (scheme)
(define (keep pred stuff)
(cond
((word? stuff)
(if (empty? stuff)
""
(if (pred (first stuff))
(word (first stuff) (keep pred (bf stuff)))
(keep pred (bf stuff)))))
(else
(if (empty? stuff)
@yao2030
yao2030 / substrings.scm
Created November 17, 2012 03:11
A substring is a subset whose letters come consecutively in the original word.
(define (substrings wd)
(if (empty? wd)
(se "")
(se (sub-helper wd) (substrings (bf wd)))))
(define (sub-helper wd)
(if (= 1 (count wd))
(se wd)
(se wd (sub-helper (bl wd)))))
@yao2030
yao2030 / palindrome.scm
Created November 17, 2012 02:56
A palindrome is a sentence that reads the same backward as forward.
(define (pal-helper wd)
(cond ((<= (count wd) 1) #t)
((equal? (first wd) (last wd))
(and #t (pal-helper (bl (bf wd)))))
(else #f)))
(define (palindrome? sent)
(pal-helper (accumulate word sent)))
@yao2030
yao2030 / roman-value.scm
Created November 15, 2012 06:45
roman-value
(define (roman-value letter)
(cond ((equal? letter 'i) 1)
((equal? letter 'v) 5)
((equal? letter 'x) 10)
((equal? letter 'l) 50)
((equal? letter 'c) 100)
((equal? letter 'd) 500)
((equal? letter 'm) 1000)
(else 'huh?)))
@yao2030
yao2030 / arabic.scm
Created November 15, 2012 06:45
conver roman numeral numbers to arabic
(define (arabic sent)
(if (= (count sent) 1)
(roman-value (first sent))
(if (< (roman-value (first sent)) (roman-value (first (bf sent))))
(+ (roman-value (first sent)) (- (first (bf sent))) (arabic (bf (bf sent))))
(+ (roman-value (first sent)) (arabic (bf sent))))))
@yao2030
yao2030 / RelativePrime.java
Created November 2, 2012 02:31
test whether an integer M and another M are relative primes.
public class RelativePrime
{
public static int max(int M, int N)
{
return M >= N ? M : N;
}
public static int min(int M, int N)
{
return M <= N ? M : N;
}
@yao2030
yao2030 / Binomial.java
Created November 2, 2012 02:30
using java to implement binomial coefficient
public class Binomial
{
public static int[][] binomial(int N)
{
int[][] a = new int[N+1][N+1];
for (int i = 1; i <= N; i++)
for (int j = 1; j <= i; j++)
{
if (i == j)