C
C#

help

❔ circular primes

TT3CH2/3/2023
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?
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
HHenkypenky2/3/2023
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;
}
}
}
TT3CH2/4/2023
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;
}
}
}
HHenkypenky2/4/2023
your rotate method
AAccord2/5/2023
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
❔ WPF Cannot find child when it's thereHi! I am working on my own RoundedCorners attachedproperty however I have stumbled upon a problem Pr❔ HttpClient exiting program during GetStringAsync() and GetAsync()I'm sending a GET to an API, but when debugging, as soon as you step off the last line, it cuts out ✅ Homework Helphttps://i.gyazo.com/f23d758d46581bcfb84f88d4178cd484.png What should I return from the FillArray() ❔ force a ushort value?if i type for example var num = 25L; i am forcing a long value (int64) how can i force a ushort in ❔ Doing post method with entity frameworki'm making a simple API to practice with entity framework but when i try to make a post to a table w❔ I can't reference a class library please helpI can't reference a class library i t says compilation error❔ ASP.NET REMOTE DEVELOPMENTHey does anyone know if I would need nginx for remote development on a ubuntu server from my windowException/NULL/Error handling advice.DeserializeAsync clearly returns a nullable generic object. From the picture we can clearly see that❔ ASP.NET Core web app vs ASP.net web application(.NET Framework)Hi, Ive been working with c# for a while now and would like to make a webapp but I dont know where t✅ IGNORE PEOPLEmy place for testing commands❔ Game Hubhi, how can i program my own game hub, like battle.net where i can download and start my own games ?❔ ✅ beginner class and propertiesi am doing basic program abt class and getting information from input but on the weight section whe❔ Best practice to retrieve MySQL columnsHello wonderful people! Can you please help me understanding what would be the best practice to ret❔ Auto Slide Showhere my code✅ materialDesign:Cardfor some reason it says that closing matrialcard doesent exist and it shows an error❔ Why does this code give the CS8604 warning?I'm confused why the below code gives the CS8604 warning. As far as I am concerned, I am null checki✅ Having trouble adding a picture to my code.what should the path look like?❔ Producer Consumer ISourceBlock<T>, periodically flushing consumed dataHi, I've the following scenario: I've a camera continuously acquiring frames and a c# app receiving ❔ Load async data and display it (WindowsAppSDK / WinUI3)I am currently developing my first windows app with C#, Windows App SDK and WinUI3. I want to do som❔ Best Approach to Implement Authentication and Authorization in Microservices with ASP.NET CoreHi friends, I'm looking for experience share. I have Microservices architecture project, that uses