Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

質數與尤拉函數

DZone's Guide to

質數與尤拉函數

·
Free Resource
// 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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}