[Community Puzzle] Should Bakers be Frugal?

This is the simple geometry of a square and a circle. You just need to remember the school formulas, and a drop of logic.

1 Like

i do remember them but i canā€™t get the output right.
Take the second test, for example:
the area of the squares is 144 ;the area of each biscuit is 7.07
144/7.07 = 20.3 so the wasteful baker will make 20 biscuits.
the remaining area of the square is 144 - 7.7*20 = 2.6
so there is no more enough are for the frugal baker to make another biscuit therefore the frugal baker will also make 20 biscuits. so the output should be 0,but itā€™s accually 4,how?
please explain this to me i canā€™t understand what iā€™m doing wrong here.

When a Baker has a flattened-out square of dough (ā€œDoughSquareā€), he will cut-out biscuits (in grid-style orderly columns and rows) until no more biscuits can be cut-out

144/7.07 = 20.3 so the wasteful baker will make 20 biscuits.

How do you cut out 20 biscuits from a square grid?

Oh, okay so heā€™s going to cut 16 biscuits in that case, but how will the frugal baker cut 4 more?

Ali-Kanbar, I do not want to suggest explicitly, this is unsportsmanlike.
But take a piece of paper in a cage. Draw a square with the indicated dimensions.
And carefully draw circles on it with the indicated sizes, just like a wasteful baker would act ā€¦
Think carefully about when the actions of bakers start to differ.

1 Like

at least tell me if iā€™m on the right track.
after the baker cuts 16 biscuits he will have an area of 30.8 left inside the square,he can reform that area to form a square with 5.56 inch a side, so he will only be able to make 1 more biscuits not 4,how am i wrong?please just tell me this

On the right one. But what will a frugal baker do after cutting out 1 biscuit?

Iā€™ve solved this problem and Iā€™m looking at other peopleā€™s solutions, I didnā€™t realize there was a solution that avoided using loops, can somebody explain the math behind the super simple solutions?

The easy approach is to use a while | doā€¦until loop.
Another option is to use recursion. (I did use it).

Writing the loop operation into a one-line function is possible but looks like it is a waste of effort to deal with this trivial problem. More importantly, the resulting code is doomed to be hard to understand and maintain, quite a lethal short-coming in the working field.

When calculating the number of biscuits by the frugal baker, it reminded me of that famous problem about buying bottled drinks:

You have x bottle caps initially.
You can exchange y bottle caps for 1 bottled drink.
After drinking 1 bottled drink, you get 1 bottle cap.
How many bottled drink can you eventually buy?

:warning: Spoiler alert :warning:

Click here

Someone solved the problem above with O(1) time complexity by finding the minimum n that satisifies xāˆ’n(yāˆ’1)<y. This method works for this puzzle too.

1 Like

Hi, here is the math explanation for O(1) solution:
:warning: Spoiler alert :warning:

Click here

Denote the initial area of dough as a, the area of one biscuit as b, and the area of biscuit cutter as c.
Denote the maximum number of biscuits as n (which is the answer).
After making n biscuits, the reduced area of dough is n*b.
If we can make n biscuits, then this inequality must be satisfied: a-n*bā‰„c.
Our goal is to find the minimum n that satisfies a-n*b<c.
The solution for n is int((a-c)/b+1), where int() denotes rounding down.

1 Like

if next dough layer is not a square, then Furgal baker can make even more cookies :smiley:

If I remove the cast to int inside the pow() method where I calculate how many biscuits the wasteful baker makes, my solution will fail on tests.

Code

int wasteful = (int) Math.pow((int) (side / diameter), 2);

But at the same time, when I calculate how many biscuits the frugal baker will make at each step, I donā€™t use the cast to int inside the pow() method, and with this combination I get 100%, and the question arises: do the tests correctly account for all cases?

Code

int biscuits = (int) Math.pow(Math.sqrt(doughArea) / diameter, 2);