CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2009
    Posts
    5

    Squared Digit Length

    I'm a beginner at C++ and I seem to be having problems with loops and I can't tell where I'm going wrong.

    Here is my task:

    I need to find the squared digit length of an integer. This means taking an input integer and add up the squares of its digits. This results in another integer. I have to repeat the loop until the result becomes either 1 or 4.

    For example if I enter a value of 85 the following would be the calculations:

    8² + 5² = 89
    8² + 9² = 145
    1² + 4² + 5² = 42
    4² + 2² = 20
    2² + 0² = 4

    Therefore the squared digit length of 85 is 5 because it took 5 steps before the result became either 1 or 4.

    So far I've figured out the code for splitting the digits.

    int squared_digit(int n)
    {
    int result = 0;
    while (n > 0)
    {
    int LD = n%10;
    result = result + (LD*LD);
    n = n/10;
    }
    return result;
    }

    However, I don't know how to loop the result. So if I type in 85 I only get:


    8² + 5² = 89

    But the rest of the calculations won't loop.
    8² + 9² = 145
    1² + 4² + 5² = 42
    4² + 2² = 20
    2² + 0² = 4

  2. #2
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Squared Digit Length

    I not really understand your problem.
    Thanks for your help.

  3. #3
    Join Date
    Apr 2009
    Posts
    5

    Re: Squared Digit Length

    I don't know how to explain it any better....

    Basically I type in a number

    for example 85

    stage 1. Split the integer into 8 and 5
    stage 2. Then square the digits and add them together (8^2 = 64) + (5^2=25) = result
    stage 3. Repeat stages 1 and 2 to the result

    I keep repeating stages 1 and 2 until the result becomes 1.

    My problem is I can't get it to repeat stages 1 and 2.

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Squared Digit Length

    The condition is

    Code:
    while ( result != 1 && result != 4)
    {
      // Split the number into and square. 
    }
    Hope this help
    Thanks for your help.

  5. #5
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Squared Digit Length

    Do you want arbitrary length or fixed length of number ?

    Your loop stop due to 8² + 9² = 145 arbitrary length
    Thanks for your help.

  6. #6
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Squared Digit Length

    Here it is.

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    // ========================================
    
    // ========================================
    
    typedef unsigned long ulong;
    void SplitAndSquare(ulong&);
    void Split(const ulong&, vector<int>&);
    
    // ========================================
    int main()
    {
    	ulong digit(85);
    
    	SplitAndSquare(digit);
    	
    	return 0;
    }
    // ========================================
    void SplitAndSquare(ulong& number)
    {
    	ulong result(number);
    	vector<int> singleDigit;
    	int step(0);
    	while (result != 1 && result != 4)
    	{
    		Split(result, singleDigit);
    		result = 0;
    		for (size_t loop = 0;loop<singleDigit.size();++loop)
    		{
    			result += singleDigit[loop] * singleDigit[loop];
    		}
    
    		++step;
    	}
    	cout << "Length of square digit is " << step; 
    }
    // ========================================
    void Split(const ulong& number, vector<int>& singleDigit)
    {
    	singleDigit.clear();  
    	ulong modifier(number);
    	while (modifier)
    	{
    		singleDigit.push_back(modifier &#37; 10);
    		modifier /= 10;
    	}
    }
    // ========================================
    
    
    // ========================================
    
    
    // ========================================
    
    // ========================================
    Thanks for your help.

  7. #7
    Join Date
    Apr 2009
    Posts
    5

    Re: Squared Digit Length

    The length I mentioned means the number of times I have to split and square the initial value before I get either 1 or 4.

    initial value = 85

    1. 8² + 5² = 89 (Square Digit Length 1)
    2. 8² + 9² = 145 (Square Digit Length 2)
    3. 1² + 4² + 5² = 42 (Square Digit Length 3)
    4. 4² + 2² = 20 (Square Digit Length 4)
    5. 2² + 0² = 4 (Square Digit Length 5)

    Function A:
    int squared_digit(int n)
    {
    int result = 0;
    while (n > 0)
    {
    int LD = n%10;
    result = result + (LD*LD);
    n = n/10;
    }
    return result;
    }

    That is to say it takes 5 loops of Function A, before 85 becomes 4.

    5 loops = 5 Square Digit Length.

    Does that help clarify anything?

  8. #8
    Join Date
    Apr 2009
    Posts
    5

    Re: Squared Digit Length

    Quote Originally Posted by Peter_APIIT View Post
    Here it is.

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    // ========================================
    
    // ========================================
    
    typedef unsigned long ulong;
    void SplitAndSquare(ulong&);
    void Split(const ulong&, vector<int>&);
    
    // ========================================
    int main()
    {
    	ulong digit(85);
    
    	SplitAndSquare(digit);
    	
    	return 0;
    }
    // ========================================
    void SplitAndSquare(ulong& number)
    {
    	ulong result(number);
    	vector<int> singleDigit;
    	int step(0);
    	while (result != 1 && result != 4)
    	{
    		Split(result, singleDigit);
    		result = 0;
    		for (size_t loop = 0;loop<singleDigit.size();++loop)
    		{
    			result += singleDigit[loop] * singleDigit[loop];
    		}
    
    		++step;
    	}
    	cout << "Length of square digit is " << step; 
    }
    // ========================================
    void Split(const ulong& number, vector<int>& singleDigit)
    {
    	singleDigit.clear();  
    	ulong modifier(number);
    	while (modifier)
    	{
    		singleDigit.push_back(modifier % 10);
    		modifier /= 10;
    	}
    }
    // ========================================
    
    
    // ========================================
    
    
    // ========================================
    
    // ========================================
    I don't understand this, I haven't learnt C++ of this level yet.

  9. #9
    Join Date
    Apr 2009
    Posts
    5

    Re: Squared Digit Length

    Quote Originally Posted by Peter_APIIT View Post
    Do you want arbitrary length or fixed length of number ?

    Your loop stop due to 8² + 9² = 145 arbitrary length
    I don't understand the concept of arbitrary length. I haven't learnt this yet.

  10. #10
    Join Date
    Apr 2004
    Posts
    102

    Re: Squared Digit Length

    You need nested loops here.

    Set your result initially to your n. Then have an outer loop using while(result/10 > 0). The inner loop should do the brunt of the math using % and / starting with the least digit, then working until you have zero left-over.

    Peter's solution is set-up like a long integer library (that is to say, an integer library for anything that needs to be greater than your standard 32- or 64-bit datatype). If your n value stays within your standard int type, his solution is way more complex than you need.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured