プロジェクト

全般

Wiki

プロフィール

操作

ホーム - Project Euler

Problem 12

Highly Divisible Triangular Number

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1+2+3+4+5+6+7=28. The first ten terms would be:
1,3,6,10,15,21,28,36,45,55,
Let us list the factors of the first seven triangle numbers:

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?

高度整除三角数

三角数の数列は自然数の和で表わされ, 7番目の三角数は 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28 である. 三角数の最初の10項は:
1,3,6,10,15,21,28,36,45,55,
となる.

最初の7項について, その約数を列挙すると, 以下のとおり.

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28

これから, 7番目の三角数である28は, 5個より多く約数をもつ最初の三角数であることが分かる.
では, 500個より多く約数をもつ最初の三角数はいくつか.

#!r6rs
#!chezscheme

(import (chezscheme))

(define (triangular-numbers end-condition)
  (let loop ([i 1] [current 0] [result '()])
    (if (and
          (not (null? result))
          (end-condition result))
      result
      (let ([next (+ current i)])
        (loop (add1 i) next (cons next result))))))

(define (divisors num)
  (let ([check-max (isqrt num)])
    (let loop ([i 1] [result '()])
      (cond
        [(< check-max i) result]
        [(zero? (mod num i))
         (loop (add1 i) (cons* i (div num i) result))]
        [else (loop (add1 i) result)]))))

(define over500-divisors-triangular-number
  (triangular-numbers
    (lambda (lis)
      (< 500 (length (divisors (car lis)))))))

(define answer-12
  (car over500-divisors-triangular-number))

(printf "12: ~D~%" answer-12)

Noppi2023/12/29に更新 · 2件の履歴