Over a million developers have joined DZone.

質數與尤拉函數

·
// description of your code here


#include 
#include 
using namespace std;


bool isPrime(int n)
{
	int i;
	for(i=2; i<=sqrt(n); i++)
	{
		if(((n % i) == 0))
		{
			return false;
		}
	}
	return true;
}

double Euler (int n)
{
	int a=3; //分子
	int b;   //分母	
	double pi = 2;
	int counter = 1;

	while(true)
	{
		if(isPrime(a) == true)
		{
			if(((a-1) % 4) != 0)
				b = a-1;
			else
				b = a+1;

			pi = pi*a/b;
			if(counter == n) 
				return pi;
			else
			{
				counter++;
				a++;
			}
		}else
			a++;
	}

}

void main()
{
	// 測試Euler函數
	cout<
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}