0

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"
Gagantous
  • 432
  • 6
  • 29
  • 69
SakuraFreak
  • 311
  • 1
  • 11
  • Is it one function? If no, move calculation to the function first. – Valerii Oct 17 '17 at 22:57
  • You know you can do `y += 2` instead, right? Your code appears to be incomplete - why is `tot1` there? – NetMage Oct 17 '17 at 23:00
  • Oh, *y+=raiz2* was stupid for sure and tot1 is because I reused code from another challenge I did and forgot to delete it. Thanks. And is one function. Got lazy after finishing it – SakuraFreak Oct 17 '17 at 23:19

1 Answers1

2

Don't fall for the arrow-head anti-pattern.

I've extracted the main bulk of code as a function with multiple return statements. Otherwise, I only made minimal modifications to show my point.

static void Main(string[] args)
{
    double x;
    Console.WriteLine("Escreva o numero");
    x = int.Parse(Console.ReadLine());
    if(IsPrime(x))
        Console.WriteLine("eh primo", x);
    else
        Console.WriteLine("{0} nao eh primo", x);
    Console.ReadKey();
}

private static bool IsPrime(double x)
{
    double raiz;
    int y, tot1 = 0, tot2, z, raiz2;
    if (x == 2 || x == 3 || x == 5)
        return true;
    if (x % 2 == 0 || x < 7)
        return false;

    bool result = false;
    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)
        {
            result = false;//return
            raiz2 = y;
        }
        else
            if (y >= raiz2)
        {
            result = true;//return
            y += raiz2;
        }
    }
    if (y <= raiz2)
        return false;

    return result;            
}
wp78de
  • 18,207
  • 7
  • 43
  • 71
  • Does the `//return` comment are a suggestion where return could be used to improve efficiency and readability? – Phil1970 Oct 17 '17 at 23:38
  • Yes, but as I said, I made only minimal modifications and focussed on flattening the if-else structure. – wp78de Oct 17 '17 at 23:42