+ 7

🐄🌾🏠 Challenge: Cow Farm

Each year every 2 cows in your farm has 1 calf. At first, you have x number of cows, but you need to have y. How many years does it take to breed them? Example: You have 6 cows: x = 6 You need to have 12 cows: y = 12 Year 0 (initial): You have 6 cows. Year 1: 6 cows bred and had 3 calves. Now you have 9 cows. Year 2: 8 cows bred (1 cow didn't breed because it didn't have a partner) and had 4 calves. Now you have 13 cows. Answer = 2 years All languages are welcomed 😊

16th Dec 2017, 3:46 AM
Augustinas Lukauskas
Augustinas Lukauskas - avatar
11 Respuestas
16th Dec 2017, 10:58 AM
qwerty
qwerty - avatar
+ 5
Pseudo code ok? If so: Int yearsBreeding (int cur, int goal){ if ((cur>=2) AND (goal>cur)){ int numYears=0; while (cur < goal){ cur = cur + (cur/2); numYears++; } return numYears; }else{ return -1; } }
16th Dec 2017, 4:09 AM
H Chiang
19th Dec 2017, 4:17 PM
Ng Ju Ping
Ng Ju Ping - avatar
+ 3
@H Chiang Nice 👏
16th Dec 2017, 4:15 AM
Augustinas Lukauskas
Augustinas Lukauskas - avatar
+ 3
@qwerty Nice! I have never seen SoloLear challenges solved in assembly before. I didn't even know it was possible. Good work! 👏
16th Dec 2017, 5:39 PM
Augustinas Lukauskas
Augustinas Lukauskas - avatar
17th Dec 2017, 12:45 PM
Luc Hariman Randrianomenjanahary
Luc Hariman Randrianomenjanahary - avatar
+ 2
@Luk Good job :)
17th Dec 2017, 1:29 PM
Augustinas Lukauskas
Augustinas Lukauskas - avatar
+ 2
https://code.sololearn.com/cGvC2kEI43Ga/?ref=app Here's an object oriented solution! :D
19th Dec 2017, 12:40 PM
MrCoder
MrCoder - avatar
+ 2
@MrCoder Nice 👍 I don't understand object oriented programming too well :)
19th Dec 2017, 12:59 PM
Augustinas Lukauskas
Augustinas Lukauskas - avatar
+ 1
@Augustinas Lukauskas Thank you, You'll understand it soon :D
19th Dec 2017, 1:03 PM
MrCoder
MrCoder - avatar
+ 1
@Ng Ju Ping 👍👍👍
19th Dec 2017, 4:24 PM
Augustinas Lukauskas
Augustinas Lukauskas - avatar