すべてのチャレンジ
中級 0件解決済み 正答率0%

Highly Divisible Triangular Number

Find the first triangular number that has at least d divisors.

このチャレンジで学べること

  • Count divisors from a prime factorization
  • Use the formula for triangular numbers
  • Split a product of coprime factors

問題

翻訳を表示

Triangular numbers count the dots in a triangle of rows 1, 2, 3, ...: T(1) = 1, T(2) = 3, T(3) = 6, and T(k) = k(k + 1) / 2. Walk through them in order and stop at the first one with at least d positive divisors (1 and the number itself count).

Implement highly_divisible_triangular(d).

Counting divisors one by one is too slow for the larger cases. Factor the number instead: if n = p^a × q^b × ..., it has (a + 1)(b + 1)... divisors. Since k and k + 1 share no factor, you can factor them separately.

Input

d: The number of divisors needed.

Output

Return the first triangular number with at least d divisors.

制約

1 ≤ d ≤ 300

例

例 1
入力:
d = 4

期待される出力:
expected = 6
入力:
long long d = 4;

期待される出力:
long long expected = 6;
入力:
long d = 4;

期待される出力:
long expected = 6;

解説

1 has one divisor and 3 has two, but 6 has four (1, 2, 3, 6), so the answer is 6.

例 2
入力:
d = 9

期待される出力:
expected = 36
入力:
long long d = 9;

期待される出力:
long long expected = 36;
入力:
long d = 9;

期待される出力:
long expected = 36;

解説

28 has six divisors and 36 = 2² × 3² has 3 × 3 = 9 (1, 2, 3, 4, 6, 9, 12, 18, 36), so the answer is 36.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

T(k) = k(k + 1) / 2, and k and k + 1 have no common factor.

ヒント 2

Halve whichever of k and k + 1 is even, then count the divisors of each part.

ヒント 3

The divisor count of a product of coprime numbers is the product of their counts.

目安の計算量

O(K √K) time for K triangular numbers tried, O(1) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。