QuestionC++ Beginner Questions

Posts 1–6 of 6 · Page 1 of 1
C++ Beginner Questions
So i have 2 questions that need to be answered and i'm still currently stuck.

1. Given integer N, decide if it is possible to represent it as a sum of two squares of integers.

Definition
Method: IsSumOf2Squares
Parameter: unsigned int
Returns: bool
Method Signature: boolIsSumOf2Squares(unsignedintN)

Examples
1. 10
Returns True
10 = 1^2 + 3^2

2. 2
Returns True
2 = 1^2 + 1^2

3. 16
Returns True
16 = 0^2 + 4^2


2. You are given an integer N. Find the digits in this number that exactly divide N (division that leaves
0 as remainder) and display their count. For N=24, there are 2 digits (2 & 4). Both of these digits
exactly divide 24. So our answer is 2.

Note: If the same number is repeated twice at different positions, it should be counted twice,
e.g., For N=122, 2 divides 122 exactly and occurs at ones' and tens' position. So for this
case, our answer is 3. Division by 0 is undefined and will not be counted.

Definition
Method: DivideExactly
Parameter: unsigned int
Returns: unsigned int
Method Signature: unsigned intDivideExactly(unsignedintN)

Examples
1. 24
Returns 2
2 and 4 divides 24 equally.

2. 122
Returns 3
1 and 2 divides 122 equally.

3. 1024
Returns 3
1, 2 and 4 divides 1024 equally, 0 is not counted.

4. 345
Returns 2
3 and 5 divides 345 equally.



ANY help is appreciated and need this ASAP
Quote Originally Posted by Suzura View Post
So i have 2 questions that need to be answered and i'm still currently stuck.

1. Given integer N, decide if it is possible to represent it as a sum of two squares of integers.

Definition
Method: IsSumOf2Squares
Parameter: unsigned int
Returns: bool
Method Signature: boolIsSumOf2Squares(unsignedintN)

Examples
1. 10
Returns True
10 = 1^2 + 3^2

2. 2
Returns True
2 = 1^2 + 1^2

3. 16
Returns True
16 = 0^2 + 4^2


2. You are given an integer N. Find the digits in this number that exactly divide N (division that leaves
0 as remainder) and display their count. For N=24, there are 2 digits (2 & 4). Both of these digits
exactly divide 24. So our answer is 2.

Note: If the same number is repeated twice at different positions, it should be counted twice,
e.g., For N=122, 2 divides 122 exactly and occurs at ones' and tens' position. So for this
case, our answer is 3. Division by 0 is undefined and will not be counted.

Definition
Method: DivideExactly
Parameter: unsigned int
Returns: unsigned int
Method Signature: unsigned intDivideExactly(unsignedintN)

Examples
1. 24
Returns 2
2 and 4 divides 24 equally.

2. 122
Returns 3
1 and 2 divides 122 equally.

3. 1024
Returns 3
1, 2 and 4 divides 1024 equally, 0 is not counted.

4. 345
Returns 2
3 and 5 divides 345 equally.



ANY help is appreciated and need this ASAP
For the first answer, I couldn't come up with a more effective way than this:

Code:
bool IsPerfectSquare( int N ) { //Example function, you can make a different one
  for( int x = 0; (x * x) <= N ; ++x){
      if ( x*x == N )
         return true;      
  }
  return false;
}
bool IsSumOf2Squares(int N){
  for( int x = 0; x < N ; ++x){
      int y = N - x;
      
      if ( IsPerfectSquare( x ) && IsPerfectSquare ( y ) ){
          return true;
      }
  }
  return false;
}
I don't understand your point in the 2nd question though. Could you explain it a little better?
Thx for qns 1.

LOL forget about qns 2. I manage to solve it with my friend's help though, but its still confusing :/

Thanks anyways
Quote Originally Posted by Suzura View Post
Thx for qns 1.

LOL forget about qns 2. I manage to solve it with my friend's help though, but its still confusing :/

Thanks anyways
Then post your solution

Honestly these looked more like high school algebra problems than C++ question lol
qns 2 solution:

unsigned int DivideExactly(unsigned int N)
{
// Refer to Part 5 of Assignment 01
int A;
int B;
unsigned int C = N;
unsigned int D;

A = 0;

if (N >= 0)
{
for (B = 1; B <= 10; B++)
{
D = C % 10;

if (D != 0 && N % D == 0)
{
++A;
}

C = C / 10;
}
}

N = A;
return N;

}

- - - Updated - - -

Its more math to C++ i guess
Quote Originally Posted by Suzura View Post
qns 2 solution:

unsigned int DivideExactly(unsigned int N)
{
// Refer to Part 5 of Assignment 01
int A;
int B;
unsigned int C = N;
unsigned int D;

A = 0;

if (N >= 0)
{
for (B = 1; B <= 10; B++)
{
D = C % 10;

if (D != 0 && N % D == 0)
{
++A;
}

C = C / 10;
}
}

N = A;
return N;

}

- - - Updated - - -

Its more math to C++ i guess

If you want a "shorter" version, here you go:

Code:
unsigned int DivideExactly( unsigned int N ){

unsigned int Count = 0;

for( int i = N; i ; i = ((i - i % 10) / 10) ){ //Skips the last digit (loop is valid while i is different from 0)
 int Digit = i % 10; //Gets the last digit from "i".
 if ( Digit && N % Digit == 0 ){ //If digit is different from 0 and the N is divisable by that digit
     Count++;
 }
}
return Count;

}
Posts 1–6 of 6 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us