This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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?))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |