primes.cpp

来自「该程序用以查找任意两个整数之间的所有素数。 Prime number finde」· C++ 代码 · 共 64 行

CPP
64
字号
#include <stdlib.h>
#include <iostream.h>
#include <math.h>

inline long sqrt(long i)
{
	long r,rnew=1,rold=r;

	do
	{
		rold=r;
		r=rnew;
		rnew = (r+(i/r));
		rnew >>= 1;
	}
	while(rold != rnew);
	return rnew;
}

inline int isprime(long i)
{
	long si,j;

	si = sqrt(i);

	for (j=2; (j<=si); j++)
	{
		if (i%j == 0)
			return 0;
	}

	return 1;

}

void main(int argc, char *argv[])
{
	long i,i1,i2,f=0;

	if (argc < 2)
	{
		cout << endl << endl;
		cout << "Prime number finder: Hugo Elias 1998" << endl << "http://freespace.virgin.net/hugo.elias" << endl << endl;
		cout << "Usage:" << endl << "  primes a b > primes.txt" << endl << endl;
		cout << "will find all primes between a and b" << endl << "and will write the results to the file PRIMES.TXT" << endl;
		return;
	}

	i1 = atol(argv[1]);
	i2 = atol(argv[2]);

	for (i=i1; i<i2; i++)
		if (isprime(i))
		{
			f++;
			if (f==16)
			{
				cout << endl;
				f=0;
			}
			cout << i << " ";
		}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?