Thread: split number (all possibilities)

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    split number (all possibilities)

    Hello

    I need a function that will split number this way:

    For instance if I have 1234:

    Code:
    1 2 3 4
    12 3 4
    1 23 4
    1 2 34
    12 34
    123 4
    1 234
    1234
    This means all possibilities how you can separate the number in '1234'..

    Does anyone have similar code? I'm not sure what I should write in google to search for something like that..

    Any help is highly appreciated!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,972
    In your example, the relative position of each number stays the same - is this a requirement?

    gg

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Depends on how large your number is.. The total number of ways will be 2^(number of digits-1). Use a bitmask to represent which gap has to be left untouched. For example a binary bitmask of 010 for your number 1234 would imply something like 12 34 . The middle gap is used as there is a 1 in that position while the others are not. So loop from 0 to 2^(digits-1) and put gaps at positions where there is a 1 in the number and you will get all the required ways to split your number.
    Last edited by PING; 04-13-2008 at 11:53 AM.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Codeplug View Post
    In your example, the relative position of each number stays the same - is this a requirement?

    gg
    Yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  2. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  3. split number in two..
    By peter_hii in forum C++ Programming
    Replies: 8
    Last Post: 04-01-2006, 09:21 PM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM