Skip to content

Instantly share code, notes, and snippets.

@bl0ckeduser
Created August 31, 2012 21:59
Show Gist options
  • Save bl0ckeduser/3559740 to your computer and use it in GitHub Desktop.
Save bl0ckeduser/3559740 to your computer and use it in GitHub Desktop.
Perfect numbers in Racket
#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