Sir/Madam,
How to find the prime number between 2^511 and 2^512.I have tried the below code but its not giving proper output.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
bool prime = false;
Console.WriteLine((Math.Pow(2,511)).ToString());
Console.WriteLine((Math.Pow(2, 512)).ToString());
double a = Math.Pow(2, 511);
double b=Math.Pow(2, 512);
Console.WriteLine((b-a).ToString());
while (a < b)
{
for (double i = 2; i < 10; i++)
{
if (a % i == 0)
{
prime = true;
//Console.WriteLine(i.ToString());
break;
}
}
if (prime == false)
{
// Console.WriteLine(a.ToString());
break;
}
a = a + 1;
}
Console.ReadLine();
}
}
}
Thanks in advance...
Loading

CrishPosted Dec 10, 2010, 6:01 AM
I have same problem.Thanks for this code it will help me to solve the issue of prime no.
Mike GoldPosted Dec 7, 2010, 1:33 PM
http://www.fractal-landscapes.co.uk/bigint.html
Josip JuricPosted Dec 7, 2010, 12:37 PM
and you can't use Math.Pow() because it return's long and we need BigInteger so we need our Function for potentiation and that function should look like this:
private BigInteger BetterPow(int x, int y)
{
BigInteger res=x;
for (int i = 1; i < y; i++)
{
res*=x;
}
return res;
}
EDIT!!!
And you code should look like this:
bool prime = false;
Console.WriteLine((BetterPow(2, 511)).ToString());
Console.WriteLine((BetterPow(2, 512)).ToString());
BigInteger a = BetterPow(2, 511);
BigInteger b = BetterPow(2, 512);
Console.WriteLine((b - a).ToString());
Console.WriteLine("");
//This is remainder what lefd dividing two BigIneger numbers(in BigInteger % operator isn't implemented)
BigInteger remainder;
for (BigInteger number = a; number < b; number++)
{
for (BigInteger divisor = 2; divisor < a; )
{
remainder = BigInteger.Remainder(number, divisor);
if (BigInteger.Compare(remainder, 0) == 0)
{
prime = false;
break;
}
else
{
prime = true;
break;
}
}
if (prime)
{
Console.WriteLine(number.ToString());
}
}
Console.ReadLine();
BTW. I put source so if download and try it.
Mike GoldPosted Dec 7, 2010, 11:34 AM
Mike GoldPosted Dec 7, 2010, 11:31 AM
-Mike