Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Im a cs lecturer at a local college and as its the first week i decided to give my students a bit of an entry level brain teaser.but to my surprise not a single student wrote a correct algorithm without my help so below you'll find a brief i created and im interested to see how other people would approach this issue (don't show code i just want to see your approach to the problem)

brief:

write a program to predict the number of creatures in a fictional alien invasion. An alien lays X egg each day and the eggs hatch after Y days.
If X is 3 and Y is 5, how many aliens
will there be 30 days after a single alien invades? Hint: make an array/list and use each slot to represent a day.
Put a 1 in the first position and then calculate how many eggs are laid and when they will hatch. Put this value in the correct cell and then add the value for the current day to the following one and move along

DON'T APPLY REAL WORLD LOGIC TO THE BIOLOGY OF THESE CREATURES JUST ANSWER THE QUESTION AS ITS WRITTEN!!!!!!!!!!
Posted
Updated 29-Sep-15 10:10am
v2
Comments
PIEBALDconsult 29-Sep-15 14:14pm    
Not enough information.
How many days does maturation require?
Do they reproduce sexually?
If there are two genders, then statistically what is the mixture of male/female aliens?
Do they require extracted mating rituals?
Member 12020386 29-Sep-15 14:20pm    
my fault i omitted pieces of information
assume they can breed immediately upon being hatched
they are a genderless species
no mating rituals required
assume they are an asexual species
PIEBALDconsult 29-Sep-15 14:39pm    
So each lays its eggs as soon as it hatches? That seems unlikely; eggs take time to create.
Member 12020386 29-Sep-15 14:44pm    
as stated before perhaps i should of provided additional information and applied real world logic to it but in my defense its the first week and i was trying to give them a simple programming problem(which is really mathematical) just to test there implementation skills in c++ using basic maths. if anything this is just an a example of a modified geometric series problem.bottom line don't focus to much on the literal just approach this as it is stated without additional information.
PIEBALDconsult 29-Sep-15 14:46pm    
You never get a second chance to make a first impression.

Every five days the population multiplies by four -- thirty days should be six birth-cycles -- 46 = 4096 . Whoops, rereading again.


New algorithm:
Begin with an array of suitable size.
Cell zero has value one, all others are zero.
Use a loop to iterate cells one through thirty.
On each iteration, add the value of the previous cell to the current cell, and add that value times three to the cell five positions further on.

At the moment I have 167236, but there may be a fence-post error.



Except JSOP will arrive and wipe them all out before that happens.

I might also argue that only the first alien is an alien, all the children hatched on the planet are natives.
 
Share this answer
 
v8
Comments
Patrice T 29-Sep-15 15:43pm    
Natives +5 :)
After I read all the comments I feel tempted to give this basic problem a try. So, if I understand the problem correctly, the number of aliens living on day n is the number of aliens of the previous day (nobody dies) plus the number of eggs that hatch on this day. The latter is equal to the number of eggs created on day n - 5, which is the 3 times the number of aliens that lived on day n - 5.

So I opened my favorite spread sheet program and put a 1 in cells 0 to 4. In cell 5 and following I placed the expression

cell[n-1] + 3*cell[n-5]

which then produces the following series:

day  aliens eggs
0	1	3
1	1	6
2	1	9
3	1	12
4	1	15
5	4	24
6	7	42
7	10	69
8	13	105
9	16	150
10	28	222
11	49	348
12	79	555
13	118	870
14	166	1.320
15	250	1.986
16	397	3.030
17	634	4.695
18	988	7.305
19	1.486	11.265
20	2.236	17.223
21	3.427	26.313
22	5.329	40.398
23	8.293	62.313
24	12.751	96.108
25	19.459	147.777
26	29.740	226.716
27	45.727	347.910
28	70.606	534.849
29	108.859	823.173
30	167.236	1.266.504

That wasn't that hard and didn't even take a C++ program to solve, once all aspects of the problem were correctly understood. And there are a few details that still need better specification, e.g. does the alien who lands first, produce three eggs on the very day of his landing, or the day after? Or, if m eggs hatch on a particular day, do we count the number aliens before or after these eggs hatch? So, with all due respect, as a student I would hope for a more precise statement of a problem than the one given.
 
Share this answer
 
v2
Comments
Member 12020386 29-Sep-15 18:31pm    
!!!!!!!!CONGRATULATIONS!!!!!!!! someone finally got the correct value.ill accept that i need to add additional details into my problem to give it clarity , and yes maybe it was hard to fully understand in this form but my god i thought someone would have put the pieces together hours ago.
nv3 29-Sep-15 18:35pm    
Well, it took me half an hour to understand the facts and about one minute to solve it :-) And the wrong example table you gave in your answer didn't exactly help :-)
Member 12020386 29-Sep-15 18:40pm    
and i totally agree with that but come on you guys are professionals wheres you ingenuity & creativity, i thought id throw you a few curve balls to see how you would digest the problem.
nv3 29-Sep-15 18:44pm    
I accept your good intents and realize that a teacher's life is tough sometimes.
Member 12020386 29-Sep-15 18:53pm    
what i don't think a lot of commenters understood was that this exercise was never about the problem.see i like to make my students think on there feet.even when it seems that its an impossible problem or all the information isn't there i still encourage them to try and think of ways of accomplishing these tasks without additional guidance.and i was pretty much testing the waters here by giving this problem to experts in the field .no ill will was meant towards anyone.
Here's an SQL Server Recursive Common Table Expression to do it:

SQL
WITH cte AS
(
	SELECT -1 [Day] , 1 [Aliens] , 0 [+1] , 0 [+2] , 0 [+3] , 0 [+4] , 0 [+5] , 0 [Eggs]
UNION ALL
	SELECT [Day] + 1 , [Aliens] + [+1] , [+2] , [+3] , [+4] , [+5] , ( [Aliens] + [+1] ) * 3 ,  [+2] + [+3] + [+4] + [+5] + ( [Aliens] + [+1] ) * 3
	FROM cte
	WHERE [Day] < 30
)
SELECT * FROM cte
 
Share this answer
 
v2
Comments
Patrice T 29-Sep-15 20:31pm    
+5
Sorry to use a second answer here, the discussion is now pretty big.

Dear lecturer, tell me if I am right or wrong.

Your alien lays 3 eggs on day 1.
-If the eggs hatch on same day. On which day will they hatch ? My answer is day 1.
-If the eggs hatch 1 day after. On which day will they hatch ? My answer is day 2.
-If the eggs hatch 2 day after. On which day will they hatch ? My answer is day 3.
-If the eggs hatch 3 day after. On which day will they hatch ? My answer is day 4.
-If the eggs hatch 4 day after. On which day will they hatch ? My answer is day 5.
-If the eggs hatch 5 day after. On which day will they hatch ? My answer is day 6.

I guess everyone agree on this list :)

You posted this as a part of the solution:
Quote:

day 1 1 alien 3 eggs
day 2 1 alien 6 eggs
day 3 1 alien 9 eggs
day 4 1 alien 12 eggs
day 5 4 aliens 21 eggs
day 6 7 aliens 39 eggs

From this sequence, one can see that eggs laid on day 1 are hatching on day 5, which is after 4 days, when the problem state after 5 days.

PS: defeat not accepted because we all know you are wrong and infinite arguing have no interest. :-)

[update]
Quote:
what don't you understand about every FIFTH day ,
there is nothing to understand there because it is not the question. :)
The question is:
Quote:
An alien lays X egg each day and the eggs hatch after Y days.
If X is 3 and Y is 5, how many aliens

and "after 5 days" is not the same as on "fifth day".
 
Share this answer
 
v3
Comments
Member 12020386 29-Sep-15 18:21pm    
what don't you understand about every FIFTH day , think about that for a second and let it sink in.okay, are you done , alright.now unfortunately you are still incorrect but i really do hope you gain an understanding of this problem in the next i don't know 20 years because my god it has been a slow day at the office with you drones.
nv3 29-Sep-15 18:31pm    
Your example table is unfortunately incorrect. It should read:

day aliens eggs
0 1 3
1 1 6
2 1 9
3 1 12
4 1 15
5 4 24
6 7 42
7 10 69

That would have made it clear. In your example the hatching it is not 5, but 4 days! I guess, that confused everyone.
Member 12020386 29-Sep-15 18:35pm    
i accept that things were getting heated and i might of glazed over a few things but still you managed to give a correct answer with the information provided
PIEBALDconsult 29-Sep-15 19:57pm    
"with the information provided"
You mean _in_spite_of_ the information provided. This not how teaching works.

nv3 29-Sep-15 18:41pm    
My 5 for detecting the error in the example table. It took me quite a while to figure that out.
My guess is 21646 31324 adult aliens and 9678 13953 just born on day 30 31.
My mistake: 1 day too early.

I used a spreadsheet to compute the value.
My rule is 1 event per day.
on day 1, the alien makes 3 eggs.
on day 6, the 3 eggs hatch.
on day 7, the 3 new born start to make eggs.

With X=1 and Y=1, it should give the Fibonacci sequence.

[Update]
Your sequence is wrong:
Quote:

day 1 1 alien 3 eggs
...
day 5 4 aliens 21 eggs

Eggs done on day 1 can't hatch 5 days later on day 5, there is 1 day missing.

Quote:
Quote:

day 4 1 alien 12 eggs
day 5 4 aliens 21 eggs
Your sequence imply that new born aliens are instantly able to make 3 eggs the same size as the one they just came out from. Your sequence also imply contain enough to make 1 baby and 3 eggs than contain enough to make a baby and ...

I think I know why nobody find your solution :)
 
Share this answer
 
v7
Comments
PIEBALDconsult 29-Sep-15 16:28pm    
Yeah. A hatchling lays _three_ eggs immediately after hatching from one egg. D'oh!
I think there needs to be a maturation period, but the OP states otherwise.
Patrice T 29-Sep-15 16:33pm    
My guess is that new born aliens need 1 day to make 3 eggs, just like adults.
PIEBALDconsult 29-Sep-15 16:38pm    
Tell that to the OP; they're _his_ aliens and he says they don't.
Patrice T 29-Sep-15 16:43pm    
:-)
Member 12020386 29-Sep-15 16:41pm    
IGNORE THE BIOLOGY OF THESE CREATURES ITS A NON ISSUE JUST SOLVE THE PROBLEM USING THE INFORMATION PROVIDED

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900