C programming

This is a discussion on C programming within the C Programming forums, part of the General Programming Boards category; hiiiiiiii, this program is about "FINDING THE 9 DIGIT NUMBER in which the number formed by leftmost two digits is ...

  1. #1
    naidu
    Join Date
    Oct 2004
    Posts
    10

    C programming

    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"

    Though i tried this program,i was not able to get the number which i deserved to get.
    the main problem that i am facing is i am not able to check whether all the digits in the number are different.
    Can any one of you help me this regard.

    the code is:
    #include <stdio.h>
    #include <math.h>

    void main()
    {
    bool bvalid = true;
    long l =0;
    long lMax = 2147483647;//Long.MAX_VALUE;
    long l1 =0,l2 =0,l3 =0,l4 =0,l5 =0,l6 =0,l7 =0,l8 =0,l9 =0;

    for (l=123456789 ; l < 987654321 ; l++ )
    {
    l1 = floor(l/100000000);
    l2 = floor(l/10000000) - (l1*10) ;
    l3 = floor(l/1000000) - (l1*100 + l2*10);
    l4 = floor(l/100000) - (l1*1000 + l2*100 + l3*10);
    l5 = floor(l/10000) - (l1*10000 + l2*1000 + l3*100 + l4*10);
    l6 = floor(l/1000) - (l1*100000 + l2*10000 + l3*1000 + l4*100 + l5*10);
    l7 = floor(l/100) - (l1*1000000 + l2*100000 + l3*10000 + l4*1000 + l5*100 + l6*10);
    l8 = floor(l/10) - (l1*10000000 + l2*1000000 + l3*100000 + l4*10000 + l5*1000 + l6*100 + l7*10);
    l9 = floor(l/1) - (l1*100000000 + l2*10000000 + l3*1000000 + l4*100000 + l5*10000 + l6*1000 + l7*100 + l8*10);

    long larr[9];
    larr[0] = l1;
    larr[1] = l2;
    larr[2] = l3;
    larr[3] = l4;
    larr[4] = l5;
    larr[5] = l6;
    larr[6] = l7;
    larr[7] = l8;
    larr[8] = l9;
    bvalid = true;
    for(int i = 0 ; i < 9 ; i++)
    {
    int temp = larr[i];
    for(int j = 0; j < 9 ; j++)
    {
    if( temp == larr[j] && (i != j) )
    {
    bvalid = false;
    break ;
    }
    }
    if(bvalid == false)
    break;
    }
    if( bvalid != false)
    {
    if( (l1 !=0) && (l2 !=0) && (l3 !=0) &&(l4 !=0) &&(l5 !=0) &&(l6 !=0) &&(l7 !=0) &&(l8 !=0) &&(l9 !=0) &&
    (l5 == 5) && ( fmod((l1*10 + l2),2) == 0 ) &&
    ( fmod((l1*100 + l2*10 + l3),3) == 0 ) &&
    ( fmod((l1*1000 + l2*100 + l3*10 + l4),4) == 0 ) &&
    ( fmod((l1*10000 + l2*1000 + l3*100 + l4*10 + l5),5) == 0 ) &&
    ( fmod((l1*100000 + l2*10000 + l3*1000 + l4*100 + l5*10 + l6),6) == 0 ) &&
    ( fmod((l1*1000000 + l2*100000 + l3*10000 + l4*1000 + l5*100 + l6*10 + l7),7) == 0 ) &&
    ( fmod((l1*10000000 + l2*1000000 + l3*100000 + l4*10000 + l5*1000 + l6*100 + l7*10 + l8),8) == 0 ) &&
    ( fmod((l1*100000000 + l2*10000000 + l3*1000000 + l4*100000 + l5*10000 + l6*1000 + l7*100 + l8*10 + l9),9) == 0)
    )
    {

    printf("satisfied %d \n",l);
    }
    //printf(" for %d value...requirement not satisfying", l);
    }
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    You could create an array of 10 ints and convert your number to a string. Then iterate through your string increment array[string[ctr]-'0']. If any of the 10 elements in array[] are more than 1, then that digit was used more than once.

    I went ahead and wrote my own version of the program just for fun. I won't ruin it for you by pasting the code, but here's the result I got

    itsme@itsme:~/C$ ./nums
    Number 381654720 found in 18531 tries.
    It had to check 18530 numbers before finding the correct one...not too incredibly efficient I guess.
    Last edited by itsme86; 11-18-2004 at 01:23 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    naidu
    Join Date
    Oct 2004
    Posts
    10

    itsme its 4 u

    the answer u got contains '0' at the end.But the number contains
    digits from 1 - 9.it does not contain 0.
    I was not able 2 understand u r logic.
    can explain u r logic once again

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're changing the rules. You never said anything about zero not being allowed. You said "9 digit number". Make up your mind.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    naidu
    Join Date
    Oct 2004
    Posts
    10
    sorry yaar,

    actually i forgot 2 say that before.The number is formed with the digits 1-9.
    I am sorry for the mistake

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    So that is number 381654729.
    Other numbers (that contains the 0 digit): 381654720 (already reported by itsme86), 783204165 and 801654723.

    Now I left the code as an exercise for the reader.

  7. #7
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    You might start by writing a program that creates a truth table for each part of the number (so a table of every 2 digit number divisable by 2, every 3 digit number divisable by 3, and every 4 digit number divisable by 4).

    Then filter the table of 3 digit numbers to include only those numbers which have their first 2 digits in common with a number from the first table.

    Repeat with the third table.

    etc. etc.

    Still a bit of a brute force approach I know.

    Optimisation includes letting the calculation of each table run only over elements determined from the previous table (10* each element up to 19* each element).
    This will mean a decreasing number of calculations instead of an increasing number as the number gets longer (after length = 5 or so as until that moment the table will get longer).

    Result == 381654729

    I won't post the entire code I wrote for this (in C++), it's just over 200 lines (pretty verbose, can almost certainly be condensed to half that).

  8. #8
    naidu
    Join Date
    Oct 2004
    Posts
    10
    hello friend rmps,
    tell me the logic.
    i will write the code.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do you need the logic dictated to you? Haven't you already said what it was?
    Code:
    if( left most two digits / evenly by two )
        if( left most three digits / evenly by three )
            if( left most four digits / evently by five )
                ...and so on...
    I mean just looking at that, you can do it that way, in the pure simplest form. Or just stare at it a moment. This is practicly writing itself. It basicly just TELLS you how to optimize it.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    naidu
    Join Date
    Oct 2004
    Posts
    10

    my query is how 2 check whether all the digits all different

    thanx for u r help.

    But this one i did before it self.
    I am struggling 2 find the logic to check whether all the digits r unique or not.
    so plz help me this regard

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for each digit N
        for each digit in the number
            if N is the same as this digit
                duplicate, so do whatever
    if we reach here without having done whatever, then there were no duplicates
    What seems to be the problem?

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    crudest way:
    if (n[0] != n[1] && n[0] != n[2] // etc. etc. for every possible combination) ...

    You can optimise part of that.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about a lookup table for each number that returns true or false if any numbers are the same?

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    naidu
    Join Date
    Oct 2004
    Posts
    10
    i converted the number to an array of 9 digits and then copied it 2 a duplicate and then tried 2 compare each digit by using two "for "loops .But i was unable 2 find the number which i want.
    can u try this once.
    thx

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Can you show us your code?
    If you understand what you're doing, you're not learning anything.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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