Thread: Need a little help with Cyclic Numbers Program

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079

    Need a little help with Cyclic Numbers Program [FIXED/CLOSED]

    ** Edit added some more info into a reply **
    ** Edit, I think I got it fixed, now, thanks for reading **

    First off, before I start. I am writing this program for a college class, there for I don't want any suggestions on how to write the program better, I'm only asking one specific question.
    "Why is it stopping where it is?"
    If you have a suggestion or answer that doesn't pertain to that question, then it's information I don't want to know. This is an assignment and I don't want anyone else doing the whole thing for me.

    This objective of this assignment is to write a program to find all six digit cyclic numbers. If you don't know what a cyclic number is, it's a number that when multiplied by any number (in the case of the assignment, only through 6) will produce a number with the same six digits.

    For example:

    123456 * 2 = 253461
    123456 * 3 = 365124
    123456 * 4 = 451632
    ... and so on through six. These are not correct calculations, it's just an example.

    Now that we've covered what cyclic numbers are, here is my program.

    Code:
    #include <iostream.h>
    
    int main() {
    	long sixDigitNum = 100000;
    	long digitMutlip;
    	int digiValue[6][6];     // Array[mulitiplier][digit]
    	int numAppear[6][10];    // Array[mulitiplier][number]
    	int mult = 0;
    	int number = 0;
    	
    	cout << "Checking for Cyclic Numbers. Please Wait..." << endl;
    
    	while (sixDigitNum <= 166666) {
    	
        // Process 1: Parsing Digits	
           do {
               digitMutlip = sixDigitNum * (mult + 1);
                
               digiValue[mult][5] = digitMutlip / 100000;
               digiValue[mult][4] = (digitMutlip % 100000) / 10000;
               digiValue[mult][3] = (digitMutlip % 10000) / 1000;
               digiValue[mult][2] = (digitMutlip % 1000) / 100;
               digiValue[mult][1] = (digitMutlip % 100) / 10;
               digiValue[mult][0] = (digitMutlip % 10);  	
               
               mult++;  	        
           } while (mult <= 5);
        // End Process 1
        
           mult = 0;
           cout << "Parsed Digits" << endl;
           cout << sixDigitNum << endl;
           
        // Process 2: Totaling Number Appearence
           do {
               if (digiValue[mult][5] == number)
                  numAppear[mult][number] = numAppear[mult][number] + 1;
               if (digiValue[mult][4] == number)
                  numAppear[mult][number] = numAppear[mult][number] + 1;
               if (digiValue[mult][3] == number)
                  numAppear[mult][number] = numAppear[mult][number] + 1;
               if (digiValue[mult][2] == number)
                  numAppear[mult][number] = numAppear[mult][number] + 1;
               if (digiValue[mult][1] == number)
                  numAppear[mult][number] = numAppear[mult][number] + 1;
               if (digiValue[mult][0] == number)
                  numAppear[mult][number] = numAppear[mult][number] + 1;
               
               if (number == 9) {
                  number = 0;
                  mult++;
                  }
               else
                  number++;          
           } while (mult <= 5);         
        // End Process 2
           
           mult = 0;
           number = 0;
           cout << "Totaled Appearences" << endl;
           
        // Process 3: Comparing mults
           do {
               if (numAppear[mult][number] == numAppear[mult + 1][number] &&
                   numAppear[mult][number] == numAppear[mult + 2][number] &&
                   numAppear[mult][number] == numAppear[mult + 3][number] &&
                   numAppear[mult][number] == numAppear[mult + 4][number] &&
                   numAppear[mult][number] == numAppear[mult + 5][number] &&
                   number < 9)
                   number++;
               else if (numAppear[mult][number] == numAppear[mult + 1][number] &&
                        numAppear[mult][number] == numAppear[mult + 2][number] &&
                        numAppear[mult][number] == numAppear[mult + 3][number] &&
                        numAppear[mult][number] == numAppear[mult + 4][number] &&
                        numAppear[mult][number] == numAppear[mult + 5][number] &&
                        number == 9) {
                        cout << "Found Cyclic Number: " << sixDigitNum << endl;
                        number++;
                        }
               else
                   number = 10;
           } while (number <= 9);
        // End Process 3
           cout << "Not Cyclic" << endl;  
                
           sixDigitNum++;
        } 
    		  
    	cin.get();
        	
        return 0;
    
    	}
    There are some lines of code in there that were for debugging purposes only, so I can see what is going on. They aren't permemant.

    Basically, what this code is doing is running through one number, then parsing the digits of the second number and stopping. Here is the output it's giving me.

    Code:
    Checking for Cyclic Numbers. Please Wait...
    Parsed Digits
    100000
    Totaled Appearences
    Not Cyclic
    Parsed Digits
    0
    Can anyone tell me why it's stopping at this point, and why sixDigitNum is coming out to 0 instead of 100001?

    Thanks, any reply is appreciated.
    Last edited by SlyMaelstrom; 10-19-2005 at 05:07 PM.
    Sent from my iPadŽ

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ok, I figured out a few things for you to consider. One, this program doesn't work properly. I inputted a cyclic number and it said it didn't tell me that it was cyclic, even though it is.

    Second, I found out the final zero in the output is the last digit in the last six digit number.

    For example, if the number is 100000, the digit is 0, if it's 104353, the digit is 3, and so on... why that is, I don't know.

    Third, It's not actually going through the parse digits loop the second time around, it's skipping over it, which is awkward for a do...while loop.
    Last edited by SlyMaelstrom; 10-19-2005 at 04:58 PM.
    Sent from my iPadŽ

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Code:
    int mult = 0;
    Interestingly enough, this problem can be solved using an absurdly fast algorithm.
    Last edited by Rashakil Fol; 10-19-2005 at 05:00 PM.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    If your talking about the second declaration of it, that's been changed, I just forgot to edit it in the post, thanks for noticing.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. somebody help me with this array program
    By hieugene in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2006, 05:53 AM
  4. Perfect Numbers C Program
    By SlayerBlade in forum C Programming
    Replies: 2
    Last Post: 08-28-2005, 05:11 PM
  5. C Program for Prime Numbers
    By mmuhlenb in forum C Programming
    Replies: 12
    Last Post: 02-19-2003, 04:55 AM