Iterating until int.MaxValue reached - c#

As a little test I wanted to see how long it would take to count to int.MaxValue in a C# console application. Every few hours I checked the progress. Last night when I thought the program would be done executing, it was executing back to 0. I'm not sure why that happened and I was wondering if anyone could explain it to me. What it did was that it counted to 2,147,483,647 then when this value was reached it began counting backwards to zero. Even though it appeared to be counting backwards, the value had a negative number. I wonder if I needed to use the absolute value of int.MaxValue. Anyway, I was just curious if anyone could see what I'm not seeing. Here is my code. Thanks
static void Main(string[] args)
{
int maxInt = int.MaxValue;
int numToCountTo = maxInt;
//int numToCountTo = 1000000;
try
{
Console.WriteLine(DateTime.Now);
Console.WriteLine("Press any key to begin.");
Console.ReadLine();
Stopwatch sw = new Stopwatch();
sw.Start();
for (int counter=0; counter <=numToCountTo ; counter++)
{
Console.WriteLine(counter);
}
sw.Stop();
TimeSpan ts = sw.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00 Hours}, {1:00 Minutes}, {2:00 Seconds}, {3:00 Milliseconds}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
// ("#,#") places a comma between every 3 digits
Console.WriteLine("It took " + elapsedTime + " to count to " + numToCountTo.ToString("#,#"));
Console.WriteLine("Press Enter key to continue.");
Console.ReadLine();
}
catch (Exception ex)
{
throw new Exception("Exception Reached: " + ex.Message)
}
}

Your for loop:
for (int counter=0; counter <=numToCountTo ; counter++)
is incorrect. It will execute while counter <= int.MaxValue which is ALWAYS true. When it increments it it will roll to int.MinValue and keep incrementing.
Change it to
for (int counter=0; counter < numToCountTo ; counter++)
or use a long for your loop counter:
for (long counter=0; counter <= numToCountTo ; counter++)
You can also use a do...while loop since the loop is executed before the breaking condition is evaluated:
int counter = 0;
do
{
...
counter++;
}
while(counter < numToCountTo);

It will continue because counter will never be > than int.MaxValue, if you do int.MaxValue+1 it will wrap down to int.MinValue.
Thus, your for condition is always true.
An alternative I would recommend is either use a larger datatype for your counter like long, or change your loop to:
int counter = -1;
for(;;) // while(true)
{
counter++;
Console.WriteLine("{0}", counter);
if (counter == int.MaxValue)
{
break;
}
}

When you add 1 to an Int32.MaxValue you will end up with Int32.MinValue.
int a = Int32.MaxValue;
a++; // a is now -2147483648

It's called an overflow - the integer wraps around to the lowest possible negative value.
If you want it to stop at maxInt you should replace the <= with a < - at present you can never stop the loop as counter can never be bigger can maxIni.

Related

Why won't this loop increment a variable?

I am trying to do a calculation which involves a do while loop in c#
I want to execute the loop and minus the quantum from the remainingTime array and assign the new value to timeelapsed.
My loop should execute while timeelapsed is > 0 and stop once it hits 0
However, the loop does not carry out the - quantum part of the code, i don't understand why.
static void Main(string[] args)
{
int quantum, noOfProcesses, processNumber = 0; // noOfprocesses variable is to determine array sizes, processNumber tells user what process number they input
int timeelapsed, count = 0;
Console.WriteLine("Please enter the number of processes:");
noOfProcesses = Convert.ToInt32(Console.ReadLine()); // allows numerical input
int[] bursttime = new int[noOfProcesses];
int[] arrivalTime = new int[noOfProcesses];
int[] remainingTime = new int[noOfProcesses];
for (int i = 0; i < noOfProcesses; i++)
{
Console.WriteLine("Enter Burst time for process #" + processNumber);
bursttime[i] = Convert.ToInt32(Console.ReadLine());
processNumber++;
}
processNumber = 1; // resets the process number for user interface
for (int i = 0; i < noOfProcesses; i++)
{
Console.WriteLine("Enter arrival time for process #" + processNumber);
arrivalTime[i] = Convert.ToInt32(Console.ReadLine());
processNumber++;
}
Console.WriteLine("Enter the time quantum:");
quantum = Convert.ToInt32(Console.ReadLine()); // allows numerical input
// calculations
processNumber = 0;
if (count <= noOfProcesses)
{
// remainingTime[0] = bursttime[0] + arrivalTime[0]; // burst + arrival = remaining time
for (int i = 0; i < noOfProcesses; i++)
{
do
{
remainingTime[i] = bursttime[i] + arrivalTime[i]; // burst + arrival = remaining time
timeelapsed = remainingTime[i] - quantum; // - the quantum gives whats remaining
timeelapsed = timeelapsed - quantum;
}
while (timeelapsed > 0);
Console.WriteLine("#" + processNumber + " Time taken: " + timeelapsed + "ms");
processNumber++;
count++;
//timeelapsed = bursttime[i] + arrivalTime[i];
}
}
// Console.WriteLine(arrivalTime[j]);
Console.ReadLine();
}
Please note that the noOfProcesses and processNumber variables are defined in the program i don't believe these are causing the issue.
All you are doing in this loop is recalculating the same values over and over again:
do {
remainingTime[i] = bursttime[i] + arrivalTime[i]; // burst + arrival = remaining time
timeelapsed = remainingTime[i] - quantum; // - the quantum gives whats remaining
timeelapsed = timeelapsed - quantum;
} while (timeelapsed > 0);
For example, you might see this if you use real numbers:
do {
remainingTime = 100 + 100;
timeelapsed = 200 - 10
timeelapsed = 190 - 10;
} while (timeelapsed > 0);
The loop never ends. I'm not sure exactly what your goal is, but you probably need to keep a running total, like:
remainingTime[i] += bursttime[i] + arrivalTime[i];
Or
remainingTime[i] -= bursttime[i] + arrivalTime[i];
This is really debugging and tough to decide without having the code in front of me. I'm assuming that the 'quantum' code is the section in the do part of the loop. In which case, the only reason why it isn't running is one of the three:
noOfProcesses is less than 1
timeelapsed is <= 0
It is getting to the first iteration of the loop but an exception is being thrown.
Since you're using c#, and hopefully VS, you should use the debugger and step through your program.

When arrays go awry?

I'm trying to learn C# by solving mathematical problems. For example, I'm working on finding the sum of factors of 3 or 5 in the first 1000 positive numbers. I have the basic shell of the code laid out, but it isn't behaving how I'm expecting it to.
Right now, instead of getting a single output of 23, I am instead getting 1,1,3,3,5,5,7,7,9,9. I imagine I messed up the truncate function somehow. Its a bloody mess, but its the only way I can think of checking for factors. Second, I think that the output is writing during the loop, instead of patiently waiting for the for() loop to finish.
using System;
namespace Problem1
{
class Problem1
{
public static void Main()
{
//create a 1000 number array
int[] numberPool = new int[10];
//use for loop to assign the first 1000 positive numbers to the array
for (int i = 0; i < numberPool.Length; i++)
{
numberPool[i] = i + 1;
}
//check for factors of 3 or 5 using if/then statment
foreach (int i in numberPool)
if ((i / 3) == Math.Truncate((((decimal)(i / 3)))) || ((i / 5) == Math.Truncate(((decimal)(i / 5)))))
{
numberPool[i] = i;
}
else
{
numberPool[i] = 0;
}
//throw the 0s and factors together and get the sum!
int sum = 0;
for (int x = 0;x < numberPool.Length;x++)
{
sum = sum + numberPool[x];
}
Console.WriteLine(sum);
Console.ReadLine();
//uncomment above if running in vbs
}
}
}
The foreach loop has a few errors.
If you want to modify the array you are looping through use a for loop. Also, use modulus when checking remainders.
for (int i = 0; i < numberPool.Length; i++)
{
if (numberPool[i] % 3 == 0 || numberPool[i] % 5 == 0)
{
// Do nothing
}
else
{
numberPool[i] = 0;
}
}
Modulus (%) will give the remainder when dividing two integers.
Another useful shortcut, variable = variable + x can be replaced with variable += x
Please note that there are more concise ways of doing this but since you are learning the language I will leave that for you to find.
#kailanjian gave some great advice for you but here is another way your initial logic can be simplified for understanding:
//the sum of factors
int sum = 0;
//the maximum number we will test for
int maxNum = 1000;
//iterate from 1 to our max number
for (int i = 1; i <= maxNum; i++)
{
//the number is a factor of 3 or 5
if (i % 3 == 0 || i % 5 == 0)
{
sum += i;
}
}
//output our sum
Console.WriteLine(sum);
You also stated:
Second, I think that the output is writing during the loop, instead of patiently waiting for the for() loop to finish.
Your program logic will execute in the order that you list it and won't move on to the next given command until it is complete with the last. So your sum output will only be printed once it has completed our for loop iteration.

Why is Console.WriteLine speeding up my application?

Ok so this is kind of weird. I have an algorithm to find the highest possible numerical palindrome that is a multiple of two factors who each have K digits.
The method I'm using to find the highest valid palindrome is to look at the highest possible palindrome for the number set (i.e. if k=3, the highest possible is 999999, then 998899, etc). Then I check if that palindrome has two factors with K digits.
For debugging, I thought it would be a good idea to print to the console each of the palindromes I was checking (to make sure I was getting them all. To my surprise, adding
Console.WriteLine(palindrome.ToString());
to each iteration of finding a palindrome dropped my runtime a whopping 10 seconds from ~24 to ~14.
To verify, I ran the program several times, then commented out the Console command and ran that several times, and every time it was shorter with the Console command.
This just seems weird, any ideas?
Here's the source if anyone wants to take a whack at it:
static double GetHighestPalindromeBench(int k)
{
//Because the result of k == 1 is a known quantity, and results in aberrant behavior in the algorithm, handle as separate case
if (k == 1)
{
return 9;
}
/////////////////////////////////////
//These variables will be used in HasKDigitFactors(), no need to reprocess them each time the function is called
double kTotalSpace = 10;
for (int i = 1; i < k; i++)
{
kTotalSpace *= 10;
}
double digitConstant = kTotalSpace; //digitConstant is used in HasKDigits() to determine if a factor has the right number of digits
double kFloor = kTotalSpace / 10; //kFloor is the lowest number that has k digits (e.g. k = 5, kFloor = 10000)
double digitConstantFloor = kFloor - digitConstant; //also used in HasKDigits()
kTotalSpace--; //kTotalSpace is the highest number that has k digits (e.g. k = 5, kTotalSpace = 99999)
/////////////////////////////////////////
double totalSpace = 10;
double halfSpace = 10;
int reversionConstant = k;
for (int i = 1; i < k * 2; i++)
{
totalSpace *= 10;
}
double floor = totalSpace / 100;
totalSpace--;
for (int i = 1; i < k; i++)
{
halfSpace *= 10;
}
double halfSpaceFloor = halfSpace / 10; //10000
double halfSpaceStart = halfSpace - 1; //99999
for (double i = halfSpaceStart; i > halfSpaceFloor; i--)
{
double value = i;
double palindrome = i;
//First generate the full palindrome
for (int j = 0; j < reversionConstant; j++)
{
int digit = (int)value % 10;
palindrome = palindrome * 10 + digit;
value = value / 10;
}
Console.WriteLine(palindrome.ToString());
//palindrome should be ready
//Now we check the factors of the palindrome to see if they match k
//We only need to check possible factors between our k floor and ceiling, other factors do not solve
if (HasKDigitFactors(palindrome, kTotalSpace, digitConstant, kFloor, digitConstantFloor))
{
return palindrome;
}
}
return 0;
}
static bool HasKDigitFactors(double palindrome, double totalSpace, double digitConstant, double floor, double digitConstantFloor)
{
for (double i = floor; i <= totalSpace; i++)
{
if (palindrome % i == 0)
{
double factor = palindrome / i;
if (HasKDigits(factor, digitConstant, digitConstantFloor))
{
return true;
}
}
}
return false;
}
static bool HasKDigits(double value, double digitConstant, double digitConstantFloor)
{
//if (Math.Floor(Math.Log10(value) + 1) == k)
//{
// return true;
//}
if (value - digitConstant > digitConstantFloor && value - digitConstant < 0)
{
return true;
}
return false;
}
Note that I have the Math.Floor operation in HasKDigits commented out. This all started when I was trying to determine if my digit check operation was faster than the Math.Floor operation. Thanks!
EDIT: Function call
I'm using StopWatch to measure processing time. I also used a physical stopwatch to verify the results of StopWatch.
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
double palindrome = GetHighestPalindromeBench(6);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}:{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
Console.WriteLine();
Console.WriteLine(palindrome.ToString());
Console.WriteLine();
Console.WriteLine(elapsedTime);
I have tested your code. My system is an i7-3770 3.40 GHz, quad-core with hyperthreading, so 8 cores available.
Debug build, with and without the console Writeline statement (commented out or not), in debug mode or not, the times vary from about 8.7 to 9.8 sec. As a Release build it comes down to about 6.8-7.0 sec either way. Th figures were the same inside VS and from the command line. So your observation is not reproduced.
On performance monitor with no console output I see one core at 100%, but it switches between cores 1,4,5 and 8. Without console output there is activity on other cores. Max CPU usage never exceeds 18%.
In my judgment your figure with console output is probably consistent with mine, and represents the true value. So your question should read: why is your system so slow when it's not doing console output?
The answer is: because there is something different about your computer or your project which we don't know about. I've never seen this before, but something is soaking up cycles and you should be able to find out what it is.
I've written this as an answer although it isn't really an answer. If you get more facts and update your question, hopefully I can provide a better answer.

Exit the loop after specific time in C#

I have a requirement in my project (C#, VS2010, .NET 4.0) that a particular for loop must finish within 200 milliseconds. If it doesn't then it has to terminate after this duration without executing the remaining iterations. The loop generally goes for i = 0 to about 500,000 to 700,000 so the total loop time varies.
I have read following questions which are similar but they didn't help in my case:
What is the best way to exit out of a loop after an elapsed time of 30ms in C++
How to execute the loop for specific time
So far I have tried using a Stopwatch object to track the elapsed time but it's not working for me. Here are 2 different methods I have tried so far:
Method 1. Comparing the elapsed time within for loop:
Stopwatch sw = new Stopwatch();
sw.Start();
for (i = 0; i < nEntries; i++) // nEntries is typically more than 500,000
{
// Do some stuff
...
...
...
if (sw.Elapsed > TimeSpan.FromMilliseconds(200))
break;
}
sw.Stop();
This doesn't work because if (sw.Elapsed > TimeSpan.FromMilliseconds(200)) takes more than 200 milliseconds to complete. Hence useless in my case. I am not sure whether TimeSpan.FromMilliseconds() generally takes this long or it's just in my case for some reason.
Method 2. Creating a separate thread to compare time:
Stopwatch sw = new Stopwatch();
sw.Start();
bool bDoExit = false;
int msLimit = 200;
System.Threading.ThreadPool.QueueUserWorkItem((x) =>
{
while (bDoExit == false)
{
if (sw.Elapsed.Milliseconds > msLimit)
{
bDoExit = true;
sw.Stop();
}
System.Threading.Thread.Sleep(10);
}
});
for (i = 0; i < nEntries; i++) // nEntries is typically more than 500,000
{
// Do some stuff
...
...
...
if (bDoExit == true)
break;
}
sw.Stop();
I have some other code in the for loop that prints some statistics. It tells me that in case of Method 2, the for loop definitely breaks before completing all the iterations but the loop timing is still 280-300 milliseconds.
Any suggestions to break a for loop strictly with-in 200 milliseconds or less?
Thanks.
For a faster comparison try comparing
if(sw.ElapsedMilliseconds > 200)
break;
You should do that check in the beggining of your loop and also during the processing, ("// Do some stuff" part of the code) because it is possible, for example, that processing starts at 190 (beginning of the loop), lasts 20 and ends at 210.
You could also measure average execution time of your processing (this is approximate because it relies on average time), this way loop should last 200 milliseconds or less, here is a demo that you can put in a Main method of a Console application and easily modify it for your application:
Stopwatch sw = new Stopwatch();
sw.Start();
string a = String.Empty;
int i;
decimal sum = 0, avg = 0, beginning = 0, end = 0;
for (i = 0; i < 700000; i++) // nEntries is typically more than 500,000
{
beginning = sw.ElapsedMilliseconds;
if (sw.ElapsedMilliseconds + avg > 200)
break;
// Some processing
a += "x";
int s = a.Length * 100;
Thread.Sleep(19);
/////////////
end = sw.ElapsedMilliseconds;
sum += end - beginning;
avg = sum / (i + 1);
}
sw.Stop();
Console.WriteLine(
"avg:{0}, count:{1}, milliseconds elapsed:{2}", avg, i + 1,
sw.ElapsedMilliseconds);
Console.ReadKey();
Another option would be to use CancellationTokenSource:
CancellationTokenSource source = new CancellationTokenSource(100);
while(!source.IsCancellationRequested)
{
// Do stuff
}
Use the first one - simple and have better chances to be precise than second one.
Both cases have the same kind of termination condition, so both should behave are more-or-less the same. Second is much more complicated due to usage of threads and Sleep, so I'd use first one. Also second one is much less precise due to sleeps.
There are abolutely no reasons for TimeSpan.FromMilliseconds(200) to take any significant amount of time (as well as calling it in every iteration).
Using cancellation token:
var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(15)).Token;
while (!cancellationToken.IsCancellationRequested)
{
//Do stuff...
}
I don't know if this is that exactly, but I think it's worth a try using a System.Timers.Timer:
int msLimit = 200;
int nEntries = 500000;
bool cancel = false;
System.Timers.Timer t = new System.Timers.Timer();
t.Interval = msLimit;
t.Elapsed += (s, e) => cancel = true;
t.Start();
for (int i = 0; i < nEntries; i++)
{
// do sth
if (cancel) {
break;
}
}

C# - for Loop Freezes at strange intervals

I am working on Problem 14 on Project Euler, and my code seems to freeze at random intervals for no apparent reason.
static void Main()
{
int maxNum = 0;
int maxLength = 0;
for (int x = 2; x < 1000000; ++x)
{
int num = x;
int length = 0;
while (num != 1)
{
if (num % 2 == 0)
{
num /= 2;
length++;
}
else
{
num = (3 * num) + 1;
length++;
}
}
if (length > maxLength)
{
maxLength = length;
maxNum = x;
}
}
Console.WriteLine(maxNum);
Console.ReadLine();
The number that the program hangs at is different each time I run it and doesn't seem to follow any set patterns. Any ideas on why it would be hanging like this? Thanks in advance.
I've solved it in another way, by caching the result for each step, and I've found your problem. I doubt your program ever stops.
The statement num = (3 * num) + 1 may overflow over Int32.MaxValue and result in a negative number and an infinite loop(?).
In this case, you can solve the problem by using long for your x.
If goes into infinite loop in while (num != 1).
I bet that this version doesn't freeze, there's no reason it should do that.
The Collatz sequences before you hit 1 in the inner while loop are too short to lead to a noticeable delay, and if they would that should always happen at the same numbers.
If you add console output inside the loop then this may allocate memory, and the pauses you see could be due to garbage collection.

Categories