
April 27th, 2009, 02:12 AM
#1
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

April 27th, 2009, 02:47 AM
#2
Re: Squared Digit Length
I not really understand your problem.
Thanks for your help.

April 27th, 2009, 02:56 AM
#3
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.

April 27th, 2009, 02:59 AM
#4
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.

April 27th, 2009, 03:18 AM
#5
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.

April 27th, 2009, 03:37 AM
#6
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 % 10);
modifier /= 10;
}
}
// ========================================
// ========================================
// ========================================
// ========================================
Thanks for your help.

April 27th, 2009, 05:14 AM
#7
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?

April 27th, 2009, 05:15 AM
#8
Re: Squared Digit Length
Originally Posted by Peter_APIIT
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.

April 27th, 2009, 05:17 AM
#9
Re: Squared Digit Length
Originally Posted by Peter_APIIT
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.

April 27th, 2009, 12:36 PM
#10
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 leftover.
Peter's solution is setup like a long integer library (that is to say, an integer library for anything that needs to be greater than your standard 32 or 64bit 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

Forum Rules

Click Here to Expand Forum to Full Width
OnDemand Webinars (sponsored)
