Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
63
Task/Pernicious-numbers/C-sharp/pernicious-numbers.cs
Normal file
63
Task/Pernicious-numbers/C-sharp/pernicious-numbers.cs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace PerniciousNumbers
|
||||
{
|
||||
class Program
|
||||
{
|
||||
public static int PopulationCount(long n)
|
||||
{
|
||||
int cnt = 0;
|
||||
do
|
||||
{
|
||||
if ((n & 1) != 0)
|
||||
{
|
||||
cnt++;
|
||||
}
|
||||
} while ((n >>= 1) > 0);
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
public static bool isPrime(int x)
|
||||
{
|
||||
if (x <= 2 || (x & 1) == 0)
|
||||
{
|
||||
return x == 2;
|
||||
}
|
||||
|
||||
var limit = Math.Sqrt(x);
|
||||
for (int i = 3; i <= limit; i += 2)
|
||||
{
|
||||
if (x % i == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static IEnumerable<int> Pernicious(int start, int count, int take)
|
||||
{
|
||||
return Enumerable.Range(start, count).Where(n => isPrime(PopulationCount(n))).Take(take);
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
foreach (var n in Pernicious(0, int.MaxValue, 25))
|
||||
{
|
||||
Console.Write("{0} ", n);
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
foreach (var n in Pernicious(888888877, 11, 11))
|
||||
{
|
||||
Console.Write("{0} ", n);
|
||||
}
|
||||
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue