split number (all possibilities)

This is a discussion on split number (all possibilities) within the C Programming forums, part of the General Programming Boards category; Hello I need a function that will split number this way: For instance if I have 1234: Code: 1 2 ...

  1. #1
    l2u
    l2u is offline
    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,701
    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 12:53 PM.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  4. #4
    l2u
    l2u is offline
    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, 10:27 AM
  2. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 11: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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21