Search code examples
phpintegerlanguage-concepts

Programming Concept: What is splitting a number into groups of other whole numbers?


I know there must be an official name to this but unfortunately I have forgotten the term. I have an integer which changes, lets say its 10. I want to divide this into however many groups as long as the result in each of the groups is 3. The leftover group if its not 3 should be 1 or 2 as I am only using whole numbers.

I also want to check this every 3 iterations but from the start so the non 3 comes at the end, so for 10

number = 3, $tag = col-md-4

number = 3, $tag = col-md-4

number = 3, $tag = col-md-4

number = 1, $tag = col-md-12

sorry if this is trivial


Solution

  • I think you are thinking of modulo (php modulo operator %)? It provides the remainder after performing division. For example:

    Division:

    10 / 3 = 3.333...
    

    Modulo:

    10 % 3 = 1
    

    In programming use, you can use the floor of the first (division) operation to get the number of full groups, and the modulus to get the size of the last group.

    For example, if you start with 14:

    How many groups of 3?

    floor(14 / 3) = 4
    

    How many in last group?

    14 % 3 = 2