Created
August 31, 2012 21:59
-
-
Save bl0ckeduser/3559740 to your computer and use it in GitHub Desktop.
Perfect numbers in Racket
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
#lang racket | |
; Prints out perfect numbers < 1000 | |
(define (proper-factors-of n) | |
(filter | |
(lambda (x) (= 0 (modulo n x))) | |
; n is excluded | |
(sequence->list (in-range 1 n)))) | |
(define (is-perfect? n) | |
(and | |
; proper sum requires multiple terms | |
(> (length (proper-factors-of n)) 1) | |
(= (apply + (proper-factors-of n)) n))) | |
(display | |
(filter is-perfect? | |
(sequence->list (in-range 1000)))) | |
(newline) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment