欧拉题专栏

欧拉题1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

1000以下3和5倍数和

//max以下3和5的倍数 和 ,手输入max
//Multiples of 3 and 5
#include<iostream>


using namespace std;

int GetNum(int max);

int main()
{
	int max,num;
	cin>>max;
	num = GetNum(max);
	cout<<num<<endl;
} 

int GetNum(int max)
{
	int index_max3,index_max5,count,i;
	count = 0;
	if((max%3) ==0 )//3倍数的,i的最大索引
		index_max3 = max/3-1;
	else
		index_max3 = max/3;
	if((max%5) == 0 )//5倍数的,i的最大索引
		index_max5 = max/5-1;
	else
		index_max5 = max/5;
	for(i=0;i<index_max3;i++)//count计算3倍数的和
	{
		count = count + (i+1)*3;
	}
	for(i=0;i<index_max5;i++)
	{
		if(((i+1)%3) == 0)//再计算5的倍数,加到count里
			continue;
		else
			count = count+(i+1)*5; 
	}
	return count;
	
} 

输入1000得到结果233168

欧拉题2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

斐波那契数列奇数项的和

//斐波那切数列奇数项和 ,输入最大的数,不超过这个数的奇数项和
#include<iostream>

using namespace std;

int EvenValuedTerms(double max);

int main()
{
	int max;
	int sum;
	cin>>max;
	cout<<EvenValuedTerms(max);
	return 0;
}

int EvenValuedTerms(double max)
{
	const int first=1,second=2;
	int tmp_odd,tmp_even;
	int sum;
	int i;
	tmp_odd = first;
	tmp_even = second;
	sum = tmp_even;
	for(i=0;;i++)
	{
		tmp_odd = tmp_odd+tmp_even;//下面两个数中的奇数项
		cout<<"odd="<<tmp_odd<<endl;
		if(tmp_odd < max) 
		{
			if(tmp_odd%2==0)
				sum += tmp_odd;
			cout<<"sum="<<sum<<endl;
		}
		else
			break;//超出max就跳出
		tmp_even = tmp_odd+tmp_even;//下面两个数中的偶数项
		cout<<"even="<<tmp_even<<endl;
		if(tmp_even < max)
		{
			if(tmp_even%2==0)
				sum += tmp_even;	
			cout<<"sum="<<sum<<endl;
		}
		else
			break;
	} 
	return sum;
}

输入4000000,得到结果4613732

欧拉题3

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

数60....的最大素因子

//数max的最大素因子 
#include<iostream>
#include<vector>

using namespace std;

vector<long long> Prime_Factor(long long max);


int main()
{
	long long max;
	cin>>max;
	cout<<"字节数"<<sizeof(max)<<endl;
	vector<long long> PrimeFactor;
	PrimeFactor = Prime_Factor(max);
	vector<long long>::iterator p = PrimeFactor.end()-1;
	cout<<"max="<<*p<<endl;
} 

vector<long long> Prime_Factor(long long max)
{
	long long i,tmp;
	tmp = max;
	vector<long long> PrimeFactor;
	for(i=2;i<=max;i++)//从2开始找到因子,
	{
		if(tmp%i==0)//把数的这个因子全部分解掉
		{
			PrimeFactor.push_back(i);
			cout<<"i="<<i<<endl;
			while(tmp%i==0)
				tmp = tmp/i;
			cout<<"tmp="<<tmp<<endl;
		}
	}
	cout<<"end"<<endl;
	return PrimeFactor;
}

猜你喜欢

转载自blog.csdn.net/zhangzhi2ma/article/details/82533796