Don't mind the awful coding, Started learning like 1 week ago...
Been doing the challanges from ProjectEuler.net with only the commands I know, but I've ended using way too much if-else statements for the formula i'm using...
How can I code using the same formula but cleaner/elegant?
double raiz, x;
int y, tot1 = 0, tot2, z, raiz2;
Console.WriteLine("Escreva o numero");
x = int.Parse(Console.ReadLine());
if (x == 2 || x == 3 || x == 5)
Console.WriteLine("eh primo");
else
if (x % 2 == 0 || x < 7)
Console.WriteLine("Numero {0} nao eh primo", x);
else
{
raiz = Math.Truncate(Math.Sqrt(x));
raiz2 = Convert.ToInt32(raiz);
z = Convert.ToInt32(x);
for (y = 3; y <= raiz2; y++)
{
tot1 = z / y;
tot2 = z % y;
if (tot2 == 0)
{
Console.WriteLine("{0} nao eh primo", x);
raiz2 = y;
}
else
if (y >= raiz2)
{
Console.WriteLine("{0} eh primo", x);
y+=raiz2;
}
}
if (y<=raiz2)
Console.WriteLine("{0} nao eh primo", x);
Console.ReadKey();
I'm using this formula to find a prime number:
√PrimeNumber=SquareRoot
for (x = 3; x <= SquareRoot; x++)
if PrimeNumber % X = 0
"not prime"
else if
x == SquareRoot
"prime"