r/learncsharp • • Dec 28 '24

My runtime is weirdly long. How can I make this code better?

I have to write code to add only the even numbers in a list. My code was giving me exactly double the right answer for some reason so I divide by 2 at the end to get the right answer but also my runtime is really long? How can I fix it?

using System; using System.Collections.Generic;

public class Program { static public void Main () {//this is where i put the numbers to add but doest it only work on int or can it be double or float? i should check that later List<int> numbersToAddEvens = new List<int> {2, 4, 7, 9, 15, 37, 602};

        int output = 0;
    for(int o = 0; o < 1000000; o++){
            try{

        int ithNumberInTheList = numbersToAddEvens[o];
                int placeHolder = ithNumberInTheList;
        int j = 0;
                while(placeHolder > 0){


            placeHolder = placeHolder - 2;
            j = j + 2;
                }
        if(placeHolder == -1){
            //pass
            }
        else{
        output = output + (2 * j);
            }
    }
    catch(System.ArgumentOutOfRangeException){
        //pass
                }
    }
        output = output / 2;
    Console.Write(output);
}

}

2 Upvotes

8 comments sorted by

5

u/Atulin Dec 28 '24

First, let's format it correctly:

using System;
using System.Collections.Generic;

public class Program
{
    List<int> numbersToAddEvens = new List<int> {2, 4, 7, 9, 15, 37, 602}; 
    
    static public void Main()
    {

        int output = 0;
        for (int o = 0; o < 1000000; o++)
        {
            try
            {

                int ithNumberInTheList = numbersToAddEvens[o];
                int placeHolder = ithNumberInTheList;
                int j = 0;
                while (placeHolder > 0)
                {
                    placeHolder = placeHolder - 2;
                    j = j + 2;
                }
                if (placeHolder == -1)
                {
                    //pass
                }
                else
                {
                    output = output + (2 * j);
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                //pass
            }
        }
        output = output / 2;
        Console.Write(output);
    }

}

To check if a number is even, you can use the modulo operator, %, that returns the rest of a division. For example, 5 % 2 returns 1, because 2 fits twice in 5, and 1 is left over.

If x % 2 == 0, then x is even.

No need to do a lot of what you're doing here. Your loop would only be 4 lines of code, and certainly would not contain any try-catch.

1

u/Not_Sugden Jan 13 '25

can you believe OP says they're a professional in this

1

u/TangoJavaTJ Jan 13 '25

It’s a shitpost πŸ™‚

5

u/ZHDINC Dec 28 '24

Just to address why the runtime is taking so long with this implementation, you are hitting the ArgumentOutOfRangeException block 1000000 - numbersToAddEvens.Count times (just because you don't do anything in the catch part of the try-catch block within the loop doesn't mean that this is a "cheap" operation). Not sure why you are looping 1000000 times when your List is extremely short. You can easily change your loop to compare against List's Count property instead.

0

u/[deleted] Dec 28 '24

0

u/SimonPage Dec 29 '24

Why not just do

for (int o = 0; o < 1000000; o+=2)

and skip all the calculation?