# Return Square Root as A Root and Multiplier

Page 1 of 1

## 3 Replies - 2201 Views - Last Post: 25 May 2011 - 08:16 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=233189&amp;s=5f6a1ab0c2750b5018531b6be1fd1e7f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 5
• Posts: 166
• Joined: 23-May 11

# Return Square Root as A Root and Multiplier

Posted 23 May 2011 - 06:02 PM

I am an amateur computer programmer with 2 years experience.The following source code returns the square root of a number as a square root and a multiplier (ie sqrt45 = 3 * sqrt5). It works perfectly aside from a few.....anomalies.... I have written the program (which is project 9-2 in "Introduction to Computer Science using C++", this is not a school project). I have attached the source code (below) as well as a picture of the issue. Thanks in advance for your help!

```
#include <iostream>; 	//Required for i/o
#include <iomanip>;  	//Required for i/o Manipulators
#include <conio.h>;    	//Required for clrscr (Clear Screen)
#include <dos.h>;      	//Required for DOS commands
#include <stdio.h>;	   	//
#include <math.h>;	   	//Required for Math Library Functions
#include <ctype.h>;		/*Required for Character
Analization Library Functions*/
using namespace std;
Name:			Project 9-2
Purpose: 		Calculate the Square root of a number as
a  Multiplier and a Square root

Function Prototypes*/
void calceve(int incoming_integer);

//Globals
//double PI = 3.1415926535897

main()
{
system("cls");
int integer = 0;

while(integer < 1)
{
cout<<"Please Input a Positive Integer: ";
cin>>integer;
if(integer > (32766))
{
system("cls");
cout<<"Number Too Large\n";
integer = 0;
}
}
calceve(integer); //Passes input to function calc(int);
cout<<'\n';
system("pause");
return 0;
}

void calceve(int incoming_integer)
{
double root = sqrt(incoming_integer), multiplier = 0;
int i = 0,i2;       //i is outer loop control, i2 is inner
int report = 2;
do                  // this loop checks to see if the number is a perfect square, if so
{                   // theres no reason to execute the next loop
i++;
if(i == root)
{
i = root;
i2 = 1;
report = 1;
break;
}
}while(i < root);

if (report == 2)//this checks to see if the previous loop determined
{				//the number to be a perfect square, if not, GO!;
i = 0;		//used 'i' as loop control for last loop, had to reset;
do//outer loop runs until control multiplier == root;
{

i++;
i2=0;
if (i > incoming_integer)//if the outer loop becomes larger than
{						 //the number input, the number cannot
i = 1;				 //be simplified, so break loop, and set
i2 = incoming_integer;//variables for proper output
report=0;
break;
}
while(i2 < incoming_integer-1)//inner loop runs to 1 less
{							  //than input, to keep all answers from
i2++;					  //being "1 * sqrt(input)"
multiplier = i * sqrt(i2);
if((multiplier >= root))//if multiplier >= root, break inner loop
{						//because they cannot be equal
break;
}
}
}while(multiplier != root);
}
cout<<endl<<incoming_integer<<" = "<<i<<" * sqrt("<<i2<<')';
if(!report)
{
cout<<" OR "<<sqrt(incoming_integer);
cout<<"\n\n(Cannot Be Simplified to a\nSquareroot and Multiplier)\n";
}
}

```

#### Attached File(s)

Is This A Good Question/Topic? 0

## Replies To: Return Square Root as A Root and Multiplier

### #2 Dogstopper

Reputation: 2950
• Posts: 11,217
• Joined: 15-July 08

## Re: Return Square Root as A Root and Multiplier

Posted 23 May 2011 - 06:17 PM

Welcome to </dream.in.code>!

As this is your first post here, I'll go ahead and move this topic to the C++ forum, where it belongs. Next time though, please post in the correct forum. Thanks!

### #3 NinjaCoder

Reputation: 9
• Posts: 17
• Joined: 23-May 11

## Re: Return Square Root as A Root and Multiplier

Posted 23 May 2011 - 10:23 PM

Here's an algorithm to break down the square root without using factorization. I'll leave it to you to break this down

```#include <iostream>
#include <cmath>

int main()
{
int number, power;
std::cout<<"Enter Number : ";
std::cin>>number;

for(power = ceil(sqrt(number)); power > 1 && number % (int)pow(power,2)!=0; power--){
}

std::cout<<"sqrt("<<number<<") = "<<power<<"* sqrt("<<(number/pow(power,2))<<")"<<std::endl;
std::cin.ignore();
std::cin.get();
return 0;
}

```

Reputation: 5
• Posts: 166
• Joined: 23-May 11

## Re: Return Square Root as A Root and Multiplier

Posted 25 May 2011 - 08:16 PM

NinjaCoder, on 23 May 2011 - 10:23 PM, said:

Here's an algorithm to break down the square root without using factorization. I'll leave it to you to break this down

```#include <iostream>
#include <cmath>

int main()
{
int number, power;
std::cout<<"Enter Number : ";
std::cin>>number;

for(power = ceil(sqrt(number)); power > 1 && number % (int)pow(power,2)!=0; power--){
}

std::cout<<"sqrt("<<number<<") = "<<power<<"* sqrt("<<(number/pow(power,2))<<")"<<std::endl;
std::cin.ignore();
std::cin.get();
return 0;
}

```

This was way more efficient than my method, of at the very least, it looks alot neater in code. Now i just have to look everything up and see just how it works. THANKS ALOT!!!!