using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace Csharp
{
class Program
{
public class Primes
{
private long min;
private long max;
public Primes() : this(2, 100) { }
public Primes(long minimum, long maximum)
{
if (minimum < 2) min = 2;
else min = minimum;
max = maximum;
}
public IEnumerator GetEnumerator()
{
for (long possiblePrime = min; possiblePrime <= max; ++possiblePrime) {
bool isPrime = true;
for (long possibleFactor = 2; possibleFactor <=
(long)Math.Floor(Math.Sqrt(possiblePrime)); ++possibleFactor) {
long remainderAfterDivition = possiblePrime % possibleFactor;
if (remainderAfterDivition == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
yield return possiblePrime;
}
}
}
}
static void Main(string[] args)
{
Primes primesFrom2To1000 = new Primes(2, 1000);
foreach (long i in primesFrom2To1000)
Console.Write("{0} ", i);
Console.ReadKey();
}
}
}
求给定范围内素数
最新推荐文章于 2023-08-05 17:09:47 发布