extInteger

Returns a extInteger.


Description

Extensions to Integer


Attributes Summary

Instance Methods

partitionByRatio Partitions receiver into nearest integer parts by ratio.
partitionIntoCanonicParts Partitions integer into canonic parts.
partitionIntoMaximallyEvenParts Partitions integer into m maximally even parts using the Björklund algorithm.
partitionIntoPartsLessThanDouble Partitions integer into parts less than double integer m.


Instance Methods


partitionByRatio

Partitions receiver into nearest integer parts by ratio.

Returns array of integers.

  1. Example

    10.partitionByRatio(#[1, 2]);
    [ 3, 7 ]
  2. Example

    -10.partitionByRatio(#[1, 2]);
    [ -3, -7 ]
  3. Example

    10.partitionByRatio(#[1]);
    [ 10 ]
  4. Example

    10.partitionByRatio(#[1, -1, -1]);
    [ 3, -4, -3 ]
  5. Example

    10.partitionByRatio(#[0.3, 0.1]);
    [ 8, 2 ]
  6. Example

    10.partitionByRatio(1 ! 20);
    [ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ]


partitionIntoCanonicParts

Partitions integer into canonic parts.

Returns array of one or more integers.

  1. Example

    (1..16).collect { |n| [n, n.partitionIntoCanonicParts] }.join("\n");
    [ 1, [ 1 ] ]
    [ 2, [ 2 ] ]
    [ 3, [ 3 ] ]
    [ 4, [ 4 ] ]
    [ 5, [ 4, 1 ] ]
    [ 6, [ 6 ] ]
    [ 7, [ 7 ] ]
    [ 8, [ 8 ] ]
    [ 9, [ 8, 1 ] ]
    [ 10, [ 8, 2 ] ]
    [ 11, [ 8, 3 ] ]
    [ 12, [ 12 ] ]
    [ 13, [ 12, 1 ] ]
    [ 14, [ 14 ] ]
    [ 15, [ 15 ] ]
    [ 16, [ 16 ] ]


partitionIntoMaximallyEvenParts

Partitions integer into m maximally even parts using the Björklund algorithm.

Returns array of one or more integers.

  1. Example

    16.partitionIntoMaximallyEvenParts(7);
    [ 2, 2, 3, 2, 2, 3, 2 ]


partitionIntoPartsLessThanDouble

Partitions integer into parts less than double integer m.

Returns array of one or more integers.

  1. Example

    (1..24).collect { |n| [n, n.partitionIntoPartsLessThanDouble(4)] }.join("\n");
    [ 1, [ 1 ] ]
    [ 2, [ 2 ] ]
    [ 3, [ 3 ] ]
    [ 4, [ 4 ] ]
    [ 5, [ 5 ] ]
    [ 6, [ 6 ] ]
    [ 7, [ 7 ] ]
    [ 8, [ 4, 4 ] ]
    [ 9, [ 4, 5 ] ]
    [ 10, [ 4, 6 ] ]
    [ 11, [ 4, 7 ] ]
    [ 12, [ 4, 4, 4 ] ]
    [ 13, [ 4, 4, 5 ] ]
    [ 14, [ 4, 4, 6 ] ]
    [ 15, [ 4, 4, 7 ] ]
    [ 16, [ 4, 4, 4, 4 ] ]
    [ 17, [ 4, 4, 4, 5 ] ]
    [ 18, [ 4, 4, 4, 6 ] ]
    [ 19, [ 4, 4, 4, 7 ] ]
    [ 20, [ 4, 4, 4, 4, 4 ] ]
    [ 21, [ 4, 4, 4, 4, 5 ] ]
    [ 22, [ 4, 4, 4, 4, 6 ] ]
    [ 23, [ 4, 4, 4, 4, 7 ] ]
    [ 24, [ 4, 4, 4, 4, 4, 4 ] ]