Skip to main content
Removed inappropriate use of property, as pointed out by @kai
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

Project Euler questions are primarily about math. This isn't really the place to practice object-oriented programming. In fact, trying to model the problem literally will lead you to a suboptimal solution.

The "right" algorithm is very simple, and involves no primality testing.

The Answer method could be a read-only property. II wouldn't bother with an IsFactor() method at all — if you just write it as a modulo test, there is no confusion about the order the parameters.

class ProjectEuler3
{
    private long number;

    public ProjectEuler3(long number)
    {
        this.number = number;
    }

    public long Answer
    {
        getGetAnswer()
        {
            long n = this.number;
            while (n % 2 == 0)
            {
                n /= 2;
            }
            for (long factor = 3; factor < n; factor += 2)
            {
                while (n % factor == 0)
                {
                    n /= factor;
                }
            }
            return n;
        }
    }

}

Project Euler questions are primarily about math. This isn't really the place to practice object-oriented programming. In fact, trying to model the problem literally will lead you to a suboptimal solution.

The "right" algorithm is very simple, and involves no primality testing.

The Answer method could be a read-only property. I wouldn't bother with an IsFactor() method at all — if you just write it as a modulo test, there is no confusion about the order the parameters.

class ProjectEuler3
{
    private long number;

    public ProjectEuler3(long number)
    {
        this.number = number;
    }

    public long Answer
    {
        get
        {
            long n = this.number;
            while (n % 2 == 0)
            {
                n /= 2;
            }
            for (long factor = 3; factor < n; factor += 2)
            {
                while (n % factor == 0)
                {
                    n /= factor;
                }
            }
            return n;
        }
    }

}

Project Euler questions are primarily about math. This isn't really the place to practice object-oriented programming. In fact, trying to model the problem literally will lead you to a suboptimal solution.

The "right" algorithm is very simple, and involves no primality testing.

I wouldn't bother with an IsFactor() method at all — if you just write it as a modulo test, there is no confusion about the order the parameters.

class ProjectEuler3
{
    private long number;

    public ProjectEuler3(long number)
    {
        this.number = number;
    }

    public long GetAnswer()
    {
        long n = this.number;
        while (n % 2 == 0)
        {
            n /= 2;
        }
        for (long factor = 3; factor < n; factor += 2)
        {
            while (n % factor == 0)
            {
                n /= factor;
            }
        }
        return n;
    }

}
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

Project Euler questions are primarily about math. This isn't really the place to practice object-oriented programming. In fact, trying to model the problem literally will lead you to a suboptimal solution.

The "right" algorithm is very simple, and involves no primality testing.

The Answer method could be a read-only property. I wouldn't bother with an IsFactor() method at all — if you just write it as a modulo test, there is no confusion about the order the parameters.

class ProjectEuler3
{
    private long number;

    public ProjectEuler3(long number)
    {
        this.number = number;
    }

    public long Answer
    {
        get
        {
            long n = this.number;
            while (n % 2 == 0)
            {
                n /= 2;
            }
            for (long factor = 3; factor < n; factor += 2)
            {
                while (n % factor == 0)
                {
                    n /= factor;
                }
            }
            return n;
        }
    }

}