Triperfect Numbers
April 23, 2019
This is easy enough if we steal some number-theory code from previous exercises:
(define (triperfect) (do ((n 1 (+ n 1))) (#f) (when (= (apply + (divisors n)) (* 2 n)) (display n) (newline))))
> (triperfect) 120 672 523776 ...
It takes a while to find all six. While we are waiting, we can write a function to find k-multiperfect numbers where σn = k n:
(define (multi-perfect k) (do ((n 1 (+ n 1))) (#f) (when (and (pre-qualify n) (= (apply + (divisors n)) (* k n))) (display n) (newline))))
To make things go faster, and because all of the multiperfect numbers are highly composite, we pre-qualify n to discard obviously non-multiperfect numbers:
(define (pre-qualify n) (let ((wheel (vector 1 2 2 4 2 4 2 4 6 2 6))) (let loop ((n n) (f 2) (w 0)) (cond ((< n (* f f)) #t) ((< 200 f) #f) ((zero? (modulo n f)) (loop (/ n f) f w)) (else (loop n (+ f (vector-ref wheel w)) (if (= w 10) 3 (+ w 1))))))))
The constant 200 can be modified as desired. We get a little bit farther this time:
> (multi-perfect 3) 120 672 523776 459818240 ...
And here are some 4-multiperfect numbers:
> (multi-perfect 4) 30240 32760 2178540 ...
You can run the program at https://ideone.com/H4dCfY. MathWorld has a good page at http://mathworld.wolfram.com/MultiperfectNumber.html.
Oh, com.informatimago.common-lisp.arithmetic.primes:divisors is found in https://github.com/informatimago/lisp/blob/master/common-lisp/arithmetic/primes.lisp
Here’s a solution in C that uses trial division to find factors. It would seemingly take a long time using this implementation to find the six triperfect numbers.
@programmingpraxis, can you elaborate on your “pre-qualify” filter? Your solution says “all of the multiperfect numbers are highly composite”, but the second triperfect number, 672, is not a highly composite number (according the the Wikipedia definition of highly composite, aka “anti-prime”, numbers).
Output:
@Daniel: I will answer for @programmingpraxis. All the triperfect numbers are similar to highly composite numbers. They all look like 2^nprime1prime2….. with all primes below 160. The triperfect numbers are no hcn though. Knowing this you can factorise the numbers with low prime numbers only (prequalify). Another trick is to search the sequence a, 2a, 3a, 4a, .. with a=2^n. In this way you can find even the highest triperfect numbers very quickly. I feel it is kind cheating though, as long as you cannot proof mathematically that triperfect numbers should behave like that.