C
C#2y ago
T3CH

❔ circular primes

so this program is meant to find how many circular primes there are below 1 million (should be 55), but is returning some crazy high number. where am i going wrong?
5 Replies
T3CH
T3CHOP2y ago
using System;

namespace CircularPrimes
{
class Program
{
static void Main(string[] args)
{
int circPrimes = 0;
for (int i = 2; i <= 1000000; i++)
{
int digits = Digits(i);
if (IsCircularPrime(i, digits)) //checks if circular prime number
{
circPrimes++;
}
}
Console.WriteLine(circPrimes);
}

public static bool IsCircularPrime(int input, int digits)
{
int temp = input;
for (int i = 0; i < digits; i++)
{
temp = Rotate(temp, 1);
if (IsPrime(temp))
{
return true;
}
}
return false;
}

public static bool IsPrime(int input)
{
if (input <= 1)
{
return false;
}

if (input == 2 || input == 3)
{
return true;
}

if (input % 2 == 0 || input % 3 == 0)
{
return false;
}

for (int i = 5; i < Math.Sqrt(input); i++)
{
if (input % i == 0 || input % (i + 2) == 0)
{
return false;
}
}
return true;
}

public static int Rotate(int i, int index)
{
int digits = Digits(i);
int rotated = (int)((i % Math.Pow(10, (digits - index))) * Math.Pow(10, index) + (i / Math.Pow(10, (digits - index))));
return rotated;
}

public static int Digits(long i)
{
int digits = 0;
while (i != 0)
{
i /= 10;
++digits;
}
return digits;
}
}
}
using System;

namespace CircularPrimes
{
class Program
{
static void Main(string[] args)
{
int circPrimes = 0;
for (int i = 2; i <= 1000000; i++)
{
int digits = Digits(i);
if (IsCircularPrime(i, digits)) //checks if circular prime number
{
circPrimes++;
}
}
Console.WriteLine(circPrimes);
}

public static bool IsCircularPrime(int input, int digits)
{
int temp = input;
for (int i = 0; i < digits; i++)
{
temp = Rotate(temp, 1);
if (IsPrime(temp))
{
return true;
}
}
return false;
}

public static bool IsPrime(int input)
{
if (input <= 1)
{
return false;
}

if (input == 2 || input == 3)
{
return true;
}

if (input % 2 == 0 || input % 3 == 0)
{
return false;
}

for (int i = 5; i < Math.Sqrt(input); i++)
{
if (input % i == 0 || input % (i + 2) == 0)
{
return false;
}
}
return true;
}

public static int Rotate(int i, int index)
{
int digits = Digits(i);
int rotated = (int)((i % Math.Pow(10, (digits - index))) * Math.Pow(10, index) + (i / Math.Pow(10, (digits - index))));
return rotated;
}

public static int Digits(long i)
{
int digits = 0;
while (i != 0)
{
i /= 10;
++digits;
}
return digits;
}
}
}
im pretty sure everything works up until it counts how many its returning 355112 ive tested it, the circular prime method works, its just counting wrong
Henkypenky
Henkypenky2y ago
problemas are in your rotation and iscircularprime method
namespace CircularPrimes
{
class Program
{
static void Main(string[] args)
{
int circPrimes = 0;
for (int i = 2; i <= 1000000; i++)
{
if (IsCircularPrime(i)) //checks if circular prime number
{
circPrimes++;
}
}
Console.WriteLine(circPrimes);
}

public static bool IsCircularPrime(int input)
{
int count = Digits(input);

int num = input;

while(IsPrime(num))
{
num = Rotate(num, count);

if (num == input)
{
return true;
}
}

return false;
}

public static bool IsPrime(int input)
{
if (input <= 1)
{
return false;
}

if (input == 2 || input == 3)
{
return true;
}

if (input % 2 == 0 || input % 3 == 0)
{
return false;
}

for (int i = 5; i < Math.Sqrt(input); i++)
{
if (input % i == 0 || input % (i + 2) == 0)
{
return false;
}
}
return true;
}

public static int Rotate(int num, int count)
{
int rem = num % 10;
int div = num / 10;
num = (int)((Math.Pow(10, count - 1)) * rem) + div;

return num;
}

public static int Digits(int i)
{
int digits = 0;
while (i > 0)
{
i /= 10;
digits++;
}
return digits;
}
}
}
namespace CircularPrimes
{
class Program
{
static void Main(string[] args)
{
int circPrimes = 0;
for (int i = 2; i <= 1000000; i++)
{
if (IsCircularPrime(i)) //checks if circular prime number
{
circPrimes++;
}
}
Console.WriteLine(circPrimes);
}

public static bool IsCircularPrime(int input)
{
int count = Digits(input);

int num = input;

while(IsPrime(num))
{
num = Rotate(num, count);

if (num == input)
{
return true;
}
}

return false;
}

public static bool IsPrime(int input)
{
if (input <= 1)
{
return false;
}

if (input == 2 || input == 3)
{
return true;
}

if (input % 2 == 0 || input % 3 == 0)
{
return false;
}

for (int i = 5; i < Math.Sqrt(input); i++)
{
if (input % i == 0 || input % (i + 2) == 0)
{
return false;
}
}
return true;
}

public static int Rotate(int num, int count)
{
int rem = num % 10;
int div = num / 10;
num = (int)((Math.Pow(10, count - 1)) * rem) + div;

return num;
}

public static int Digits(int i)
{
int digits = 0;
while (i > 0)
{
i /= 10;
digits++;
}
return digits;
}
}
}
T3CH
T3CHOP2y ago
im confused where am i going wrong here
using System;

namespace CircularPrimes
{
class Program
{
static void Main(string[] args)
{
int circPrimes = 0;
for (int i = 2; i <= 1000000; i++)
{
if (IsCircularPrime(i)) //checks if circular prime number
{
circPrimes++;
}
}
Console.WriteLine(circPrimes);
}

public static bool IsCircularPrime(int input)
{
int digits = Digits(input);
int temp = input;
while (IsPrime(temp))
{
temp = Rotate(temp, digits);

if (temp == input)
{
return true;
}
}
return false;
}

public static bool IsPrime(int input)
{
if (input <= 1)
{
return false;
}

if (input == 2 || input == 3)
{
return true;
}

if (input % 2 == 0 || input % 3 == 0)
{
return false;
}

for (int i = 5; i < Math.Sqrt(input); i++)
{
if (input % i == 0 || input % (i + 2) == 0)
{
return false;
}
}
return true;
}

public static int Rotate(int i, int index)
{
int digits = Digits(i);
int rotated = (int)((i % Math.Pow(10, (digits - index))) * Math.Pow(10, index) + (i / Math.Pow(10, (digits - index))));
return rotated;
}

public static int Digits(long i)
{
int digits = 0;
while (i != 0)
{
i /= 10;
++digits;
}
return digits;
}
}
}
using System;

namespace CircularPrimes
{
class Program
{
static void Main(string[] args)
{
int circPrimes = 0;
for (int i = 2; i <= 1000000; i++)
{
if (IsCircularPrime(i)) //checks if circular prime number
{
circPrimes++;
}
}
Console.WriteLine(circPrimes);
}

public static bool IsCircularPrime(int input)
{
int digits = Digits(input);
int temp = input;
while (IsPrime(temp))
{
temp = Rotate(temp, digits);

if (temp == input)
{
return true;
}
}
return false;
}

public static bool IsPrime(int input)
{
if (input <= 1)
{
return false;
}

if (input == 2 || input == 3)
{
return true;
}

if (input % 2 == 0 || input % 3 == 0)
{
return false;
}

for (int i = 5; i < Math.Sqrt(input); i++)
{
if (input % i == 0 || input % (i + 2) == 0)
{
return false;
}
}
return true;
}

public static int Rotate(int i, int index)
{
int digits = Digits(i);
int rotated = (int)((i % Math.Pow(10, (digits - index))) * Math.Pow(10, index) + (i / Math.Pow(10, (digits - index))));
return rotated;
}

public static int Digits(long i)
{
int digits = 0;
while (i != 0)
{
i /= 10;
++digits;
}
return digits;
}
}
}
Henkypenky
Henkypenky2y ago
your rotate method
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server