Thread: C programming

  1. #16
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by annamayya
    hiiiiiiii,
    this program is about "FINDING THE 9 DIGIT NUMBER in which the number formed by leftmost two digits is divisible by 2,the number formed by left most three digits is divisible by 3 and SO on.......&at last the number is divisible by 9.......The condition is
    no digit is used more than once"

    [edit]



    Post edited by Dave Evans. Thanks to istsme86 for pointing out my boo-boo.

    Lots of statements that were reallllllly wrong have been deleted here




    The smallest nine-digit number that has nine different non-zero digits is

    123456789

    The largest is

    987654321



    A brute force program does something like this:

    Code:
    int digits[9]
      for (i = 123456789; i <= 987654321; i++) {
       /* store the digits in digits[8], digits[7],... digits[0] */
    
       /* see if the number qualifies: reject any number    */
       /* with digit[m] == 0, for m = 0 .. 9                */
    
       /* for j = 0..8, k = 0..8, k != j                    */
       /* reject any number with digit[j] == digit[k]       */
     
       /* then test the products; make a loop to reject any */
       /* number for which ((digit[8]*10+digit[7] % 2) != 0 */
       /* or (digit[8]*100+digit[7]*10 +digit[6])% 3) != 0  */
       /* etc.                                              */
    
       /* print out successful candidates (or whatever)     */
      }
    (Takes a long time to run, but it's easy to see that it works. If you think of a more optimal implementation somewhere along the way, then go for it!)

    Of course, you can combine some of the steps, but keeping them separate like this may make it easier to debug. (And of course you do everything in loops instead of hard-coding a lot of comparisons.)

    You can change the number of digits so that, for example, you can do some initial debugging with three-digit numbers or four-digit numbers. It's easier to see what went wrong if you print out intermediate steps (that is, if it doesn't work perfectly the first time).
    [/edit]

    Regards,

    Dave
    Last edited by Dave Evans; 11-20-2004 at 09:04 AM.

  2. #17
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I don't think you're taking the divisibility limitation into account.

    Say, for example, that you take the number 123456789. You have to make sure that:

    12 is divisible by 2
    123 is divisible by 3
    1234 is divisible by 4
    .
    .
    .
    12345678 is divisible by 8
    123456789 is divisible by 9
    If you understand what you're doing, you're not learning anything.

  3. #18
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by itsme86
    I don't think you're taking the divisibility limitation into account.

    Say, for example, that you take the number 123456789. You have to make sure that:

    12 is divisible by 2
    123 is divisible by 3
    1234 is divisible by 4
    .
    .
    .
    12345678 is divisible by 8
    123456789 is divisible by 9
    Boy! Is my face red!!! I was calculating things based on the product of the first n digits being divisible by n. Arghhhhhhhhhhhhhhhh.


    I have edited my previous post to delete the most obvious of the bad things I said.

    The (minor) change that I made in my pseudocode resulted in a 12-second edit of the program I had previously run, and gave the correct answer (one and only one), as previously reported by rmps.

    Regards,

    Dave

    "Everything is made of little numbers, whizzing around so fast that they become ... stuff."

    ---Terry Pratchett
    The Truth
    Last edited by Dave Evans; 11-19-2004 at 01:46 PM.

  4. #19
    naidu
    Join Date
    Oct 2004
    Posts
    10

    i got it in my own style.thx all

    hiiiii,

    i got it.........
    after working 4 ten hours.
    But this is my own logic.
    I thank all who bared my mails.

    here is the code:

    #include<stdio.h>
    #include<math.h>

    int main(void)
    {
    long num;
    long l[9],ml[9];

    for(num=123456789;num<=987654321;num++)
    {
    l[0]=num/100000000;
    ml[0]=l[0];

    l[1]=num/10000000;
    ml[1]=l[1]%10;

    l[2]=num/1000000;
    ml[2]=l[2]%10;
    .
    .
    .
    .
    .

    // and so on
    // at last

    l[8]=num;
    ml[8]=l[8]%10;

    // i got the digits as well as the leftmost 2 digit number ,3 digit number ,4 digit number .....and so on

    if(ml[4]==5)// since the number formed by left most 5 digits should divisible by 5

    if(((l[1]%2)==0)&&((l[2]%3)==0)&&........check till
    ((l[8]%9)==0))
    // here the real stuff starts
    if(((ml[0]*ml[1]*ml[3]*...........*ml[8])==362880)&&((ml[0]+ml[1]+...........+ml[8])==45))
    // since we want the number containing 9 different digits and we know the numbers r 1,2,3....,8,9.i used the above logic.


    printf("%ld\n",num);
    }
    return(0);
    }

    output: 381654729

  5. #20
    naidu
    Join Date
    Oct 2004
    Posts
    10
    hiiiii,

    i got it.........
    after working 4 ten hours.
    But this is my own logic.
    I thank all who bared my mails.

    here is the code:

    #include<stdio.h>
    #include<math.h>

    int main(void)
    {
    long num;
    long l[9],ml[9];

    for(num=123456789;num<=987654321;num++)
    {
    l[0]=num/100000000;
    ml[0]=l[0];

    l[1]=num/10000000;
    ml[1]=l[1]%10;

    l[2]=num/1000000;
    ml[2]=l[2]%10;
    .
    .
    .
    .
    .

    // and so on
    // at last

    l[8]=num;
    ml[8]=l[8]%10;

    // i got the digits as well as the leftmost 2 digit number ,3 digit number ,4 digit number .....and so on

    if(ml[4]==5)// since the number formed by left most 5 digits should divisible by 5

    if(((l[1]%2)==0)&&((l[2]%3)==0)&&........check till
    ((l[8]%9)==0))
    // here the real stuff starts
    if(((ml[0]*ml[1]*ml[3]*...........*ml[8])==362880)&&((ml[0]+ml[1]+...........+ml[8])==45))
    // since we want the number containing 9 different digits and we know the numbers r 1,2,3....,8,9.i used the above logic.


    printf("%ld\n",num);
    }
    return(0);
    }

    output: 381654729

  6. #21
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Woop?

Popular pages Recent additions subscribe to a feed