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?

#include <iostream>
using namespace std;

int divi(long long a)
{
	int res = 0;
	for (int i = 1; i <= a; i++)
	{
		if (a%i == 0)
			res++;
	}
	return res;
}

int main()
{
	long long count = 0;
	for (int i = 1; i <= 10000000; i++)
	{
		count += i;
		if (divi(count) >= 500)
			break;
		//cout << i << endl;
	}
	printf("%lld\n", count);
	system("pause");
	return 0;
}

虽然能算出结果但是真的好慢。

任何整数都可以质因数分解成 N=p1^a1 * p2^a2 * p3^a3 * ...

则N的因子个数为(a1+1)*(a2+1)*(a3+1)*....

#include <iostream>
#include <map>
using namespace std;

int divi(long long a)
{
	int res = 1;
	map<int, int> mp;
	for (int i = 2; i <= a; i++)
	{
		while (a != i)
		{
			if (a%i == 0)
			{
				mp[i]++;
				a = a / i;
			}
			else
				break;
		}
	}
	if (a != 1)
		mp[a]++;
	map<int, int>::iterator iter = mp.begin();
	for (iter = mp.begin(); iter != mp.end(); iter++)
	{
		//cout << iter->first << " " << iter->second << endl;
		res = res*(iter->second + 1);
	}
	return res;
}

int main()
{
	long long count = 0;
	for (int i = 1; i <= 10000000; i++)
	{
		count += i;
		if (divi(count) >= 500)
			break;
		//cout << i << endl;
	}
	printf("%lld\n", count);
	system("pause");
	return 0;
}
时间: 2024-10-10 05:58:40

Project Euler:Problem 12 Highly divisible triangular number的相关文章

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, 2

projecteuler----&gt;problem=12----Highly divisible triangular number

title: 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

[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet

[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet Description A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagore

Project Euler: Problem 17 Number letter counts

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be

Project Euler: Problem 14 Longest Collatz sequence

The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 It can b

Project Euler problem 68

题意需要注意的一点就是, 序列是从外层最小的那个位置顺时针转一圈得来的.并且要求10在内圈 所以,这题暴力的话,假定最上面那个点一定是第一个点,算下和更新下就行. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cstdlib> #include <ctime> #include <set> #i

Project Euler:Problem 11 Largest product in a grid

In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30

Project Euler problem 69

考察欧拉函数的一道题 首先要知道 [定理]正整数n(n≥2)可以唯一分解成素数乘积,即:n =p[1]^r1 * p[2] ^r2 * p[3]^r3. *...* p[s]^rs 其次欧拉函数有两个性质,可以用来编程,单独求phi函数: ① phi(m) =  m ( 1- 1/p[1]) ( 1- 1/p[2])-( 1- 1/p[s]) ② phi(p^k) = p^k – p^(k-1) = (p-1)p^(k-1) 然后 m/phi(m)  = (p[1] *p[2]*p[3]*

Project Euler: Problem 9 Special Pythagorean triplet

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. 首先联立式子,消除c,得到关于ab的等式5