I am learning scheme to get something new from a programming language and this code below is the solution to Project Euler question 21 but the code runs 10x slower than the listed Python code when I use Chibi-Scheme. Can any Scheme craftsman refactor the code into the Scheme idiom for efficiency.Scheme Code:(define (sum-of-amicable-pairs n)  (let ((sums (make-vector n)))        (for-each          (lambda (i)             (vector-set! sums i                         (reduce + 0                                  (filter (lambda (j) (= (remainder i j) 0))                                          (iota (+ 1 (quotient i 2)) 1 1)))))             (iota n 0 1))        (let loop ((res (make-vector n 0))                   (i 0))          (cond            ((= i n) (reduce + 0 (vector->list res)))            ((and (< (vector-ref sums i) n) (not (= (vector-ref sums i) i))                   (= (vector-ref sums (vector-ref sums i)) i))             (begin               (vector-set! res i i)               (vector-set! res (vector-ref sums i) (vector-ref sums i))               (loop res (+ 1 i))))            (else              (loop res (+ i 1)))))))(display (sum-of-amicable-pairs 10000))(newline)Python code:def amicable_pairs(n):    returns sum of all amicable pairs under n. See project euler for     definition of an amicable pair    div_sum = [None]*n    amicable_pairs_set = set()    for i in range(n):        div_sum[i] = sum([j for j in range(1, i/2 + 1) if i%j == 0])    for j in range(n):        if div_sum[j] < n and div_sum[div_sum[j]] == j and div_sum[j] != j:            amicable_pairs_set.add(j)            amicable_pairs_set.add(div_sum[j])    #print sum(amicable_pairs_set)    return sum(list(amicable_pairs_set))
A little scheme programming challenge
python;optimization;project euler;scheme;programming challenge
I would optimize the python code first and then translate it to the scheme (sorry, can't find Scheme compiler for Windows). Here is python solution that 100-200 times faster (the idea based on the Sieve of Eratosthenes algorithm):python25\python 21.pyamicable_pairs      : 31626  Time (s): 2.313000amicable_pairs_fast : 31626  Time (s): 0.015000python26\python 21.pyamicable_pairs      : 31626  Time (s): 1.969000amicable_pairs_fast : 31626  Time (s): 0.016000python27\python 21.pyamicable_pairs      : 31626  Time (s): 1.782000amicable_pairs_fast : 31626  Time (s): 0.000000python32\python 21.pyamicable_pairs      : 31626  Time (s): 3.157000amicable_pairs_fast : 31626  Time (s): 0.015000pypy\pypy 21.pyamicable_pairs      : 31626  Time (s): 0.157000amicable_pairs_fast : 31626  Time (s): 0.015000. import time    http://projecteuler.net/problem=21    Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).    If d(a) = b and d(b) = a, where a !=  b, then a and b are an amicable pair and each of a and b are called amicable numbers.    For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284.    The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.    Evaluate the sum of all the amicable numbers under 10000.    http://codereview.stackexchange.com/questions/13476/a-little-scheme-programming-challengedef amicable_pairs(n):    returns sum of all amicable pairs under n. See project euler for    definition of an amicable pair    div_sum = [None]*n    amicable_pairs_set = set()    for i in range(n):        div_sum[i] = sum([j for j in range(1, int(i/2) + 1) if i%j == 0])    for j in range(n):        if div_sum[j] < n and div_sum[div_sum[j]] == j and div_sum[j] != j:            amicable_pairs_set.add(j)            amicable_pairs_set.add(div_sum[j])    #print sum(amicable_pairs_set)    return sum(list(amicable_pairs_set))def amicable_pairs_fast(n):    div_sum = [1]*n    for i in range(2, int(n/2) + 1):        for j in range(i, n, i):            if j != i:                div_sum[j] += i    total = 0    for i in range(1, n):        s = div_sum[i]        if s < n:           s1 = div_sum[s]           if  s1 == i and s != i:              total += s1    return totalt = time.time()print(amicable_pairs      : %d  % amicable_pairs(10000) +  Time (s): %f  % (time.time()-t))t1 = time.time()print(amicable_pairs_fast : %d  % amicable_pairs_fast(10000) +  Time (s): %f  % (time.time()-t1))