-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare_supplies.py
46 lines (37 loc) · 1.29 KB
/
square_supplies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
"""
Square supplies
===============
With the zombie cure injections ready to go, it's time to start treating
our zombified rabbit friends (known as zombits) at our makeshift zombit
treatment center. You need to run out really fast to buy some gauze pads
but you only have 30 seconds before you need to be back.
Luckily, the corner store has unlimited gauze pads in squares of all
sizes. Jackpot! The pricing is simple - a square gauze pad of size K x K
costs exactly K * K coins. For example, a gauze pad of size 3x3 costs 9
coins.
You're in a hurry and the cashier takes a long time to process each
transaction. You decide the fastest way to get what you need is to buy
as few gauze pads as possible, while spending all of your coins (you can
always cut up the gauze later if you need to). Given that you have n
coins, what's the fewest number of gauze pads you can buy?
Write a method answer(n), which returns the smallest number of square
gauze pads that can be bought with exactly n coins.
n will be an integer, satisfying 1 <= n <= 10000.
Test cases
==========
Inputs:
(int) n = 24
Output:
(int) 3
Inputs:
(int) n = 160
Output:
(int) 2
"""
def answer(n):
bought = 0
while n > 0:
n = n - math.floor(math.sqrt(n))**2
bought += 1
return bought