Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

sequence 1 2 3 4 5 6=12

Does anyone know how to write a formula for "list possibilities of sequencing 1, 2, 3, 4, 5 and 6 to equal 12. any combination, any repetition, any omission"?


Rob.

MacBook, OS X Mountain Lion (10.8.2), 4 GB ram iWeb '09 3.0.4

Posted on Feb 22, 2013 8:06 AM

Reply
20 replies

Feb 22, 2013 4:52 PM in response to indigorob

In response to Badunit's "What you are asking takes recursion or plain old brute force."

indigorob wrote:


I still want it.


Here it is then.



What you are asking is a list of the base 7 numerals from 000000000066 to 111111111111, filtered to show only those whose digits sum to 12, base 10 (or 15, base 7).


Relatively easy to do via the brute force route, but you will find it tedious and time consuming. Here's the first part of the first table and the filtering rule, shown after filtering.

User uploaded file

To construct Table 1:


Open a new Blank template.


Click on any cell to make the table active.


Use the Column control handle (top right corner of table) to resize by adding or deleting columns until you have columns A through N.


Use the Row control handle (bottom left) to add rows to the table until the last row is number 10001.


Enter the value and formulas in the cells indicated:


Seed value (for table 1):


A1: 46


Formulas: (Note that the formula in A2 is a slight revision of the one shown above the table in the image.)


A2: =NUMTOBASE(RIGHT("000000000000"&ROW()+$A$1,7,12))

B2: =VALUE(MID($A2,COLUMN()-1,1))

Fill Right to M2.

N2: =SUM(B2:M2)


Click the row reference tab for Row 2 to select that row, then Copy.


With Row 2 still selected, scroll to the bottom of the table.


Shift-click the row reference tab for Row 10001 to select all cells from B2 to N10001. Paste


When the beachball stops spinning, A10001 should contain 000000041202, and N10001 should contain the sum of the digits of that number (9).


Click the Reorganize button to open the Reorganize panel. In the middle section, choose "Column N", "is", and enter 12 in the box to the right of the two menus. Click the check box if it is still unchecked.


When the beachball stops spinning, the first few rows of the table should like the image above (except that the cells in column N will not be blue filled).


At this point you can print the first series of sequences meeting your specifications.


Next set:


To produce the next set you need only to replace the 'seed value', 46 with the value 10046, then wait for the calculations to complete.


Seed numbers for subsequent sets are:


20046

30046

40046

50046

etc.

Until you reach 660000000000 in column A


Expressed in base 10, that's


13,558,811,952



Enjoy.


Regards,

Barry

Feb 22, 2013 4:58 PM in response to indigorob

Hello


Not that it's done by formulae in Numbers.


The following AppleScript script will yield the permutations or combinations according to a flag. Currently it returns all permutations (1936 cases). If you want combinations only, set property list_all_permutations in main() handler to false.


It will put the result as text in clipboard so that you can paste it into Numbers. Each line in result represents a sequence in the form of space delimited elements. The order of sequences is determined by the cource of algorithm and not in dictionary order. Sort it in Numbers if necessary.


Regards,

H


(*
    additive decomposition of given integer by given elements

    list combinations only or list all permutations
*)
main()
on main()
    script o
        property list_all_permutations : true -- true = list all permutations; false = list combinations only
        property pp : {}
        property qq : {}
        property rr : {}

        repeat
            display dialog "sum; additive elements" default answer "12; 1 2 3 4 5 6"
            --display dialog "sum; additive elements" default answer "12; 6 5 4 3 2 1"
            try
                tell words of text returned of result
                    set n to item 1 as integer
                    set pp to rest
                end tell
                repeat with p in pp
                    set p's contents to p as integer
                end repeat
                exit repeat
            end try
        end repeat
        --return {n, pp}

        if list_all_permutations then
            set qq to additiveDecomposition(n, pp, {_repeated:true})
            repeat with q in my qq
                set my rr to my rr & permutation(q, count q)
            end repeat
        else
            set rr to additiveDecomposition(n, pp, {_repeated:true})
        end if
        --return rr

        set r to _array2text(rr, space, return)
        set the clipboard to r
        return r
    end script
    tell o to run
end main

on additiveDecomposition(n, xx, _opt)
    (*
        integer n: positive integer to be decomposed
        list xx: base set as list of (unique) positive integers (in ascending order) [*1]
        record _opt: {_repeated:boolean}, whether to allow repeated element in decomposition
        return list: list of additive decompositions of n

        [1] if _opt = {_repeated:true}, xx should be unique integers without duplicates
            if _opt = {_repeated:false}, xx must be integers in ascending order
    *)
    script o
        property pp : xx
        property rr : {}
        property _repeated : _opt's _repeated

        on kernel(a, ix, qq) -- recursion kernel
            (*
                int a: positive integer to be decomposed
                int ix: maximum index of base set to test
                list qq: carried list of additive decompositions candidates

                property pp : base set
                property rr : result list
            *)
            local p
            repeat with i from ix to 1 by -1
                set p to my pp's item i
                if p = a then set end of my rr to qq & p
                if p < a then
                    if _repeated then
                        kernel(a - p, i, qq & p)
                    else
                        kernel(a - p, i - 1, qq & p)
                    end if
                end if
            end repeat
        end kernel

        kernel(n, count my pp, {})
        return my rr's contents
    end script
    tell o to run
end additiveDecomposition

on permutation(xx, n) -- v1.0f1
    (*
        list xx: list of items
        int n: number of items in each permutation
        return list: list of permutations
    *)
    script o
        property pp : xx's contents
        property ppl : (count my pp)
        property qq : {}
        property rr : {}

        if n = 0 then return {}
        if n = 1 then
            repeat with p in my pp
                set p to p's contents
                if {p} is not in my qq then set end of my qq to p
            end repeat
            return my qq's contents
        end if

        repeat with i from 1 to ppl
            set p to my pp's item i
            if {p} is in my qq then -- prune this branch
            else
                if i = 1 then
                    set yy to {}
                else
                    set yy to my pp's items 1 thru (i - 1)
                end if
                if i < ppl then set yy to yy & my pp's items (i + 1) thru -1
                -- yy = pp - {p}
                repeat with r in permutation(yy, n - 1)
                    set end of my rr to {p} & r's contents
                end repeat
                set end of my qq to p -- add to prune list
            end if
        end repeat
        return my rr's contents
    end script
    tell o to run
end permutation

on _array2text(aa, cs, rs)
    script o
        property pp : aa
        property qq : {}
        repeat with p in my pp
            set end of my qq to _join(cs, p's contents)
        end repeat
        return _join(rs, qq)
    end script
    tell o to run
end _array2text

on _join(d, tt)
    local astid, astid0, t
    set astid to a reference to AppleScript's text item delimiters
    try
        set {astid0, astid's contents} to {astid's contents, {d}}
        set t to "" & tt
        set astid's contents to astid0
    on error errs number errn
        set astid's contents to astid0
        error errs number errn
    end try
    return t
end _join

Feb 22, 2013 6:29 PM in response to Hiroto

Hello


I just noticed the sortedness of elements of base set is not required at all in any case while the uniqueness of elements of base set is required when _opt = {_repeated:true}. So here's a revised safer version of additiveDecomposition() handler which pre-processes the given base set so as to guarantee the uniqueness of the elements when _opt = {_repeated:true}.


Regards,

H


on additiveDecomposition(n, xx, _opt)
    (*
        integer n: positive integer to be decomposed
        list xx: base set as list of positive integers.
        record _opt: {_repeated:boolean}, whether to allow repeated element in decomposition
        return list: list of additive decompositions of n
    *)
    script o
        property pp : xx
        property rr : {}
        property _repeated : _opt's _repeated
        
        on uniq(aa)
            (*
                list aa : source list
                return list : list of unique items of aa
            *)
            script o
                property xx : aa
                property yy : {}
                repeat with x in my xx
                    set x to x's contents
                    if {x} is not in my yy then set end of my yy to x
                end repeat
                return my yy's contents
            end script
            tell o to run
        end uniq
        
        on kernel(a, ix, qq) -- recursion kernel
            (*
                int a: positive integer to be decomposed
                int ix: maximum index of base set to test
                list qq: carried list of additive decompositions candidates
                
                property pp : base set
                property rr : result list
            *)
            local p
            repeat with i from ix to 1 by -1
                set p to my pp's item i
                if p = a then set end of my rr to qq & p
                if p < a then
                    if _repeated then
                        kernel(a - p, i, qq & p)
                    else
                        kernel(a - p, i - 1, qq & p)
                    end if
                end if
            end repeat
        end kernel
        
        -- pre-process base set (pp)
        if _repeated then set pp to uniq(my pp)
        
        -- invoke recursion kernel
        kernel(n, count my pp, {})
        
        return my rr's contents
    end script
    tell o to run
end additiveDecomposition

Feb 22, 2013 11:19 PM in response to MlchaelLAX

Hi Michael,


The OP's first post asked for "any combination, any repetition, any omission", which would make your two examples the same.


But a later post describes the target as "possibilities of interval permutations equalling one octave for music", which would make 6 3 1 2 and 6 3 2 1 distinct examples.


I suspect permutations is what is looked for, as there would certainly be a recognizable difference on hearing, say, 1 3 6 2 then 3 2 1 6.


I think my own interpretation went beyond that, I'm afraid. after fiddling with constructing combinations for a while, I realized that permutations made more sense, but also looked at each series having exactly 13 notes, with 12 intervals between them, each of which could range from 0 (same note repeated to 6 ('half' octave). As noted in my previous post, this leads to a table far beyond the maximum 64k rows, and the necessity to reuse the table several (more than 'several', actually) times to calculate all the possibilities.


I've gone back to looking at combinations and a tabular solution to finding them. Made some progress, but it seems a problem that will require a lot of 'steeping' time while I'm busy with something else. I expect the scripters will have refined their solutions before I finish, as they appear to have conquered the combinations solution already.


Regards,

Barry

sequence 1 2 3 4 5 6=12

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.