glasswizzard
Well-known member
- Joined
- Nov 22, 2019
- Messages
- 126
- Programming Experience
- Beginner
Here is a problem I have just solved, but I'd like to know why it happened in the first place. Here is a console program from the web that populates a list of prime numbers:
It works fine. I have a program where I manually populate a list of primes and I used the above code to replace my huge list of PrimeList.Add method calls. Here is how the relevent parts of my code look, this is the list itself:
It is instantiated in the constructor:
I put the main code I got from the web into the PopulatePrimeList() method:
You may notice apart from using shorts instead of longs I have also added a condition for the for loop, that being "&& checkValue > 0". I had to add this because the loop would run forever, I used Debug.WriteLine to see what was happening and the checkValue variable would end up going from one end of it's maxvalue to the other and just kept cycling through.
When I saw this it made me think that somehow checkValue ended up at it's maxvalue - 1, thereby going over it's limit when it's incremented by 2, and then wrapping back around from the other end. This gave me the idea to make sure it's always above zero. But when I saw that the max value for a short is an odd number I realized my thinking was wrong, a shorts maxvalue - 1 will always be an even number and since checkedValue starts at 3 and is incremented by 2 it can never be an even number. So my idea on what was going wrong was itself wrong, but my solution did work.
I'm very curious to know if anyone can tell why this happened?
C#:
using System;
using System.Collections.Generic;
namespace PrimeFactorCalculator
{
/// <summary>
/// This program will calculate and populate a list of prime factors.
/// </summary>
class Program
{
private static List<long> _primes = new List<long>();
static void Main(string[] args)
{
// Manually add the first prime number.
_primes.Add(2);
Console.WriteLine(2);
for (long checkValue = 3; checkValue <= long.MaxValue; checkValue += 2)
{
if (IsPrime(checkValue))
{
_primes.Add(checkValue);
Console.WriteLine(checkValue);
}
}
}
private static bool IsPrime(long checkValue)
{
bool isPrime = true;
foreach (long prime in _primes)
{
if ((checkValue % prime) == 0 && prime <= Math.Sqrt(checkValue))
{
isPrime = false;
break;
}
}
return isPrime;
}
}
}
It works fine. I have a program where I manually populate a list of primes and I used the above code to replace my huge list of PrimeList.Add method calls. Here is how the relevent parts of my code look, this is the list itself:
C#:
public static List<short> PrimeList { get; }
It is instantiated in the constructor:
C#:
static NumberPropertiesModel()
{
PrimeList = new List<short>();
PopulatePrimeList();
// more code
}
I put the main code I got from the web into the PopulatePrimeList() method:
C#:
public static void PopulatePrimeList()
{
// Manually add the first prime number.
PrimeList.Add(2);
for (short checkValue = 3; checkValue <= short.MaxValue && checkValue > 0; checkValue += 2)
{
if (IsPrime(checkValue))
{
PrimeList.Add(checkValue);
}
}
}
private static bool IsPrime(short checkValue)
{
bool isPrime = true;
foreach (short prime in PrimeList)
{
if ((checkValue % prime) == 0 && prime <= Math.Sqrt(checkValue))
{
isPrime = false;
break;
}
}
return isPrime;
}
You may notice apart from using shorts instead of longs I have also added a condition for the for loop, that being "&& checkValue > 0". I had to add this because the loop would run forever, I used Debug.WriteLine to see what was happening and the checkValue variable would end up going from one end of it's maxvalue to the other and just kept cycling through.
When I saw this it made me think that somehow checkValue ended up at it's maxvalue - 1, thereby going over it's limit when it's incremented by 2, and then wrapping back around from the other end. This gave me the idea to make sure it's always above zero. But when I saw that the max value for a short is an odd number I realized my thinking was wrong, a shorts maxvalue - 1 will always be an even number and since checkedValue starts at 3 and is incremented by 2 it can never be an even number. So my idea on what was going wrong was itself wrong, but my solution did work.
I'm very curious to know if anyone can tell why this happened?