Algorithm to divide a chocolate bar in equal parts

后端 未结 3 720
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-03 10:14

A random thought popped into my head (when I was sharing a chocolate bar of course!). I was wondering if there is a generic algorithm to solve this problem.

The problem

3条回答
  •  悲&欢浪女
    2021-02-03 10:58

    It seems to me that you're looking for numbers that are evenly dividable by all numbers between 1 and n inclusive. That's called the least common multiple of 1, ..., n. A square containing the least common multiple of 1, ..., n squares would by definition be evenly dividable into pieces of size 1, ..., n. You're looking for a maximum of n splits, which adds additional complexity to the problem which may or may not be possible.

    Your example for n = 4 is the LCM(4,3,2,1) which is 12. LCM(5,4,3,2,1) is 60. LCM(6,5,4,3,2,1) is also 60.

    They can always be laid out as 1xLCM(n,...,1) rectangles, and always be dividable into 1,...,n even piles in n-1 or fewer divisions.

    For example, when n = 4, LCM(4,3,2,1) = 12. The rectangle is

    ############
    

    And can be divided as follows:

    1: ############     // 0 cuts
    2: ###### ######    // 1 cut
    3: #### #### ####   // 2 cuts
    4: ### ### ### ###  // 3 cuts (3 being n-1)
    

提交回复
热议问题