5 ms·
> I agree with this criticism, so I replaced the program: That makes more sense, but I still don't understand the expression a % ((cc % b)+1) == 0 If I c
by jules 14y ago
> I agree with this criticism, so I replaced the program:
That makes more sense, but I still don't understand the expression
a % ((cc % b)+1) == 0
If I change it to something that would make sense to me, I get vastly different results:
for a in range(2,24):
total = 0
for b in range(1,30):
for c in range(1,1000):
cc = c + start_offset
cicada_hatch = (cc % a == 0)
predators_hatch = (cc % b == 0)
if cicada_hatch and predators_hatch:
total += 1
print ' %2d = %6d %s' % (a,total, '*' * (total/320))
But now the fast cycles get large bars. What we want to know is how often the cicada mate vs how often they are eaten: (written overly verbosely to make sure it's readable)
for a in range(2,24):
total_cicada_hatchings = 0
total_predator_feasts = 0
for b in range(1,30):
for c in range(1,1000):
cc = c + start_offset
cicada_hatch = (cc % a == 0)
predators_hatch = (cc % b == 0)
if cicada_hatch:
total_cicada_hatchings += 1
if cicada_hatch and predators_hatch:
total_predator_feasts += 1
efficiency = float(total_cicada_hatchings) / float(total_predator_feasts)
print ' %2d = %6d %s' % (a,efficiency, '*' * int(efficiency * 5))
With this code indeed the prime numbers show up. But this code has the problem I described: both the predator cycle and the cicada cycle start synchronized. Here is the fix:
initial_cicada_cycle_year = 1 # arbitrary number
initial_predator_cycle_year = 0 # another arbitrary number
for a in range(2,24):
total_cicada_hatchings = 0
total_predator_feasts = 0
for b in range(1,30):
# cicada_cycle_year will have a value in the range {0..a-1}
cicada_cycle_year = initial_cicada_cycle_year % a
# predator_cycle_year will have a value in the range {0..b-1}
predator_cycle_year = initial_predator_cycle_year % b
for c in range(1,1000):
cicada_hatch = cicada_cycle_year == 0
predators_hatch = predator_cycle_year == 0
if cicada_hatch:
total_cicada_hatchings += 1
if cicada_hatch and predators_hatch:
total_predator_feasts += 1
# move the cycle forward by 1 year
cicada_cycle_year = (cicada_cycle_year + 1) % a
predator_cycle_year = (predator_cycle_year + 1) % b
efficiency = float(total_cicada_hatchings) / float(total_predator_feasts)
print ' %2d = %6d %s' % (a,efficiency, '*' * int(efficiency * 5))
If you set initial_cicada_cycle_year equal to initial_predator_cycle_year then you'll get the prime numbers to show up. Otherwise you get something else.
> The reason is that what's being measured is the number of collisions between a particular predator reproductive cycle over time, and a valid result merits a long time (I usually use 1000 years). This fact prevents the starting point from influencing the outcome.
Suppose that the predator cycle is 3 and the cicada cycle is 3. Then if at year 0 the cycles coincide, they will in the future coincide every 3 years because you have this pattern:
year 0123456789 ...
cicada X X X X ...
predator X X X X ...
If at the start year 0 the cycles do not coincide, they will never coincide in the future, no matter for how many years you run the simulation:
year 0123456789 ...
cicada X X X X ...
predator X X X ...
So the starting offset between the cycles is highly important!
> including a valiant effort by psychologists and fans of psychology to delete my Wikipedia page (an effort that will eventually probably succeed)
Haha that is so funny...and sad. Also my apologies for the tone in my original post. The reference to psychology was unnecessary and mean..
- lutusp 14y ago> So the starting offset between the cycles is highly important! You're missing something basic here. The program's starting point is irrelevant because it's modeling a natural continuous process, one that stretches out to infinity in the past and the future (sort of like an indefinite integral). So there isn't a "starting point" in nature, only in these models. And, because I model all the predator cycles between 1 and 30 in order to get a single result for one experimental cicada cycle, and because I model 1000 years, I have eliminated the starting point from consideration. In nature, some predators are born in year 1, some in year 2 and so forth. Some predators have a 2-year reproductive cycle, some 3, and so forth. Those two factors are independent, and one of them answers the objection you're making (the arbitrary starting point). But to make sure we're on the same page, here's my latest algorithm: https://www.dropbox.com/s/7m8hq9yzcu5nztx/prime_reproduction_test.py https://www.dropbox.com/s/7m8hq9yzcu5nztx/prime_reproduction... Note that I changed the basic algorithm to agree with that used in my Web page. It doesn't change the result, but it's perhaps easier to understand. This version arbitrarily adds a random number 0 <= cr <= 99 to the starting year of the predator on each test, much like your original code except that my way of doing it "wraps around" within the period determined by variable b. This effort to randomize things doesn't change the result rankings -- primes always come out ahead (in the sense of fewer collisions between predators and cicadas). Sample output: 2 = 18 ****************** 3 = 18 ****************** 4 = 25 ************************* 5 = 16 **************** 6 = 41 ***************************************** 7 = 18 ****************** 8 = 37 ************************************* 9 = 29 ***************************** 10 = 45 ********************************************* 11 = 20 ******************** 12 = 52 **************************************************** 13 = 20 ******************** 14 = 33 ********************************* 15 = 33 ********************************* 16 = 38 ************************************** 17 = 19 ******************* 18 = 57 ********************************************************* 19 = 25 ************************* 20 = 56 ******************************************************** 21 = 32 ******************************** 22 = 37 ************************************* 23 = 21 ********************* The present form is just one of many variations I tried. I used random numbers to tweak various starting points, all with the same outcome -- primes are always favored. Because modulo arithmetic is being used, one needs to add random numbers carefully, with a clear understanding of their effect.