Skip to content

Instantly share code, notes, and snippets.

@kimarx
Created October 8, 2010 15:01
Show Gist options
  • Save kimarx/616930 to your computer and use it in GitHub Desktop.
Save kimarx/616930 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/gosh
;;; Last Update: 2010-10-09 00:01+09:00.
;;; Project Euler - Problem-55:
;;; http://projecteuler.net/index.php?section=problems&id=55
;;; Kim, Yi-Chul < kimarx@gmail.com >
(use srfi-1)
(use srfi-13)
(print (lychrel-counter 0 (iota 10000 1)))
(define (reverse-num n)
(string->number
(string-reverse
(number->string n))))
;;; Add a given number to its reversed number.
(define (add-rev n)
(+ n (reverse-num n)))
(define (palindromic-num? n)
(eqv? n (reverse-num n)))
(define (rec-palindromic? init n)
(let ((num init))
(cond [(> num 50) #f]
[(palindromic-num? n) #t]
[else (rec-palindromic? (+ num 1) (add-rev n))])))
(define (lychrel? n)
(not (rec-palindromic? 1 (add-rev n))))
(define (lychrel-counter init lis)
(let ((count init))
(if (null? lis)
count
(if (lychrel? (car lis))
(lychrel-counter (+ count 1) (cdr lis))
(lychrel-counter count (cdr lis))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment