7 kyu

Simple Fun #189: Plant Doubling

274 of 499myjinxin2015

Description:

Task

Lonerz got some crazy growing plants and he wants to grow them nice and well.

Initially, the garden is completely barren.

Each morning, Lonerz can put any number of plants into the garden to grow.

And at night, each plant mutates into two plants.

Lonerz really hopes to see n plants in his garden.

Your task is to find the minimum number of plants Lonerz has to plant to get exactly n plants one day.

Example

For n = 5, the output should be 2.

Lonerz hopes to see 5 plants. He adds 1 plant on the first morning and on the third morning there would be 4 plants in the garden. He then adds 1 more and sees 5 plants.

So, Lonerz only needs to add 2 plants to his garden.

For n = 8, the output should be 1.

Lonerz hopes to see 8 plants. Thus, he just needs to add 1 plant in the beginning and wait for it to double till 8.

Input/Output

The number of plant lonerz hopes to see in his garden.

  • [input] integer n

    1 <= n <= 10^7

  • [output] an integer

The number of plants Lonerz needs to plant.

Puzzles

Stats:

CreatedMar 8, 2017
PublishedMar 8, 2017
Warriors Trained1189
Total Skips25
Total Code Submissions1731
Total Times Completed499
JavaScript Completions148
C# Completions60
Python Completions274
Ruby Completions35
Haskell Completions29
Total Stars13
% of votes with a positive feedback rating93% of 152
Total "Very Satisfied" Votes134
Total "Somewhat Satisfied" Votes15
Total "Not Satisfied" Votes3
Total Rank Assessments5
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • jhoffner Avatar
  • hobovsky Avatar
  • cliffstamp Avatar
  • Fuzzi Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad