Created
October 8, 2010 15:01
-
-
Save kimarx/616930 to your computer and use it in GitHub Desktop.
This file contains 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
#!/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