-2

I am trying to write a function for creating a prime factors list. For this - I am using a recursive function. Here are the calls:

    private int Problem003()
    {
        //The prime factors of 13195 are 5, 7, 13 and 29.
        //What is the largest prime factor of the number 600851475143 ?

        return GeneratePrimeFactorsList(15).Last();
    }

    private List<int> GeneratePrimeFactorsList(int n)
    {
        List<int> _primefactors = new List<int>();
        _primefactors.Add(2);

        int i = 3;

        while(i <= n)
        {   
            if (CheckIfIntIsPrime(_primefactors, i))
            {   
                _primefactors.Add(i);
            }

            i=i+2;
        }

        return _primefactors;
    }

    private bool CheckIfIntIsPrime(List<int> _primefactors, int i)
    {   
        if (_primefactors.Count() == 0)
        {   
            return true;
        }
        else
        { 
            if(i % _primefactors.First() != 0)
            {
                _primefactors.Remove(_primefactors.First());
                return CheckIfIntIsPrime(_primefactors, i);
            }
            else
            {   
                return false;
            }
        }
    }

The problem is that, when I am calling for CheckIfIntIsPrime(List, i), which have bool return type - it modifies the List. That means, that after the check - the passed argument into the GeneratePrimeFactorsList(int) is getting empty after each while loop iteration.

The CheckIfPrime function works correctly, but modifies the passed argument, when it should not - I dont relate them.

Very strange case, but I feel, that I missing knowledge about some of the List properties.

10
  • 2
    The code as is would lead to infinite recursion. Commented Oct 25, 2016 at 18:56
  • 1
    Non-primitive types are not copied. If you want to work on copy, preferably create local list from passed reference by calling List<int> local = new List<int>(_primefactors); Commented Oct 25, 2016 at 18:56
  • @OndrejTucny dont agree - code does not fall into infinte loop. Commented Oct 25, 2016 at 18:58
  • if (_primefactors.Count() == 0) should always be false as you never remove anything from the list. Commented Oct 25, 2016 at 18:58
  • 2
    Are ILists passed by value? Commented Oct 25, 2016 at 19:06

1 Answer 1

1

An object is a reference type. All variables that are typed as a reference type don't contain the value itself; they each contain a reference or pointer to the value (or object). This is true even when the variable is passed as a parameter.

The function that receives the object parameter is not capable of modifying the variable itself (which, again, is just a pointer). This means it cannot cause the variable to point somewhere else. However, it can use the pointer to get a reference to the object and modify the object itself.

If you want to pass an object but want to make absolutely sure it doesn't get modified, you can pass a clone of it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.