简体   繁体   中英

Can't figure out how to program in outputs for the given situations

So, I'm working on an assignment for my intro to computer science class. The assignment is as follows.

There is an organism whose population can be determined according to the following rules:

The organism requires at least one other organism to propagate. Thus, if the population goes to 1, then the organism will become extinct in one time cycle (eg one breeding season). In an unusual turn of events, an even number of organisms is not a good thing. The organisms will form pairs and from each pair, only one organism will survive If there are an odd number of organisms and this number is greater than 1 (eg, 3,5,7,9,…), then this is good for population growth. The organisms cannot pair up and in one time cycle, each organism will produce 2 other organisms. In addition, one other organism will be created. (As an example, let us say there are 3 organisms. Since 3 is an odd number greater than 1, in one time cycle, each of the 3 organisms will produce 2 others. This yields 6 additional organisms. Furthermore, there is one more organism produced so the total will be 10 organisms, 3 originals, 6 produced by the 3, and then 1 more.)

A: Write a program that tests initial populations from 1 to 100,000. Find all populations that do not eventually become extinct.

Write your answer here:

B: Find the value of the initial population that eventually goes extinct but that has the largest number of time cycles before it does.

Write your answer here:

The general idea of what I have so far is (lacking sytanx) is this with P representing the population

int generations = 0;

{

if (P is odd) //I'll use a modulus modifier to divide by two and if the result is not 0 then I'll know it's odd

P = 3P + 1

else

P = 1/2 P

generations = generations + 1

}

The problem for me is that I'm uncertain how to tell what numbers will not go extinct or how to figure out which population takes the longest time to go extinct. Any suggestions would be helpful.

Basically what you want to do is this: wrap your code into a while-loop that exits if either P==1 or generations > someMaxValue.

Wrap this construct into a for-loop that counts from 1 to 100,000 and uses this count to set the initial P.

If you always store the generations after your while-loop (eg into an array) you can then search for the greatest element in the array.

This problem can actually be harder than it looks at the first sight. First, you should use memorization to speed things up - for example, with 3 you get 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 -> 0, so you know the answer for all those numbers as well (note that every power of 2 will extinct).

But as pointed out by @Jerry, the problem is with the generations which eventually do not extinct - it will be difficult to say when to actually stop. The only chance is that there will (always) be a recurrence (number of organisms you already passed once when examining the current number of organisms), then you can say for sure that the organisms will not extinct.

Edit: I hacked a solution quickly and if it is correct, you are lucky - every population between 1-100,000 seems to eventually extinct (as my program terminated so I didn't actually need to check for recurrences). Will not give you the solution for now so that you can try by yourself and learn, but according to my program the largest number of cycles is 351 (and the number is close to 3/4 of the range). According to the google search for Collatz conjecture, that is a correct number (they say 350 to go to population of 1, where I'm adding one extra cycle to 0), also the initial population number agrees.

One additional hint: Check for integer overflow, and use 64-bit integer (unsigned __int64, unsigned long long) to calculate the population growth, as with 32-bit unsignet int, there is already an overflow in the range of 1-100,000 (the population can indeed grow much higher intermediately) - that was a problem in my initial solution, although it did not change the result. With 64-bit ints I was able to calculate up to 100,000,000 in relatively decent time (didn't try more; optimized release MSVC build), for that I had to limit the memo table to first 80,000,000 items to not go out of memory (compiled in 32-bit with LARGEADDRESSAWARE to be able to use up to 4 GB of memory - when compiled 64-bit the table could of course be larger).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM