Question
A positive integer is said to be square-free if none of its factors (greater than 1) are themselves squares. For example, the positive integer 30
A positive integer is said to be square-free if none of its factors (greater than 1) are themselves squares.
For example, the positive integer 30 has factors 1, 2, 3, 5, 6, 10, 15, and 30. None of these factors (greater than 1) are themselves squares, and so 30 is square-free.
For another example, the positive integer 36 has factors 1, 2, 3, 4, 9, 12, 18, and 36. Notice that 3 of these factors (greater than 1) are themselves squares: 4 (since 4 is 2 * 2), 9 (since 9 is 3 * 3), and 36 (since 36 is 6 * 6). Therefore, 36 is not square-free.
An ordered pair of positive integers can be classified in one of four ways:
If both of the integers in the pair are square-free, then this pair is SWEET.
If the first integer in the pair is square-free but the second one is not, then this pair is SOUR.
If the second integer in the pair is square-free but the first one is not, then this pair is SALTY.
If neither of the integers in the pair are square-fee, then this pair is BITTER.
Write, compile, execute, and test a C++ program that asks the user to input an ordered pair of integers, each between 2 and 5000. Then, for each integer of the pair, it should display that integer's factors (greater than 1) that are themselves squares, and also display whether or not that integer is square-free. Finally, it should display whether this ordered pair of integers is SWEET, SOUR, SALTY, or BITTER.
Here is an example of how your program should look when run:
Enter the 1st integer of the pair, between 2 and 5000: 30
Enter the 2nd integer of the pair, between 2 and 5000: 36
30 has these factors (>1) that are square: (none)
30 is square-free
36 has these factors (>1) that are square: 4, 9, 36
36 is not square-free
Therefore, the ordered pair (30, 36) is SOUR.
Here is the code, but it always gives me this message for line 47. "error a function definition is not allowed here before '{' token." please help me fix this. Line 47 is the { right under int solve(int num1).
#include
using namespace std;
//--------------
int is_perfect_squre(int n);
int solve(int num1);
int main()
{
int num1,num2;
//User have to give Input in desire range if not then it will ask for input again.
while(true)
{
cout<<"Enter the 1st integer of the pair, between 2 and 5000: ";
cin>>num1;
cout<<"Enter the 2nd integer of the pair, between 2 and 5000: ";
cin>>num2;
if(num1 < 2 or num1>5000 or num2 < 2 or num2>5000)
{
cout<<"Invalid pair. Please give input in between 2 and 5000. "<
}
else
{
break;
}
}
int flag1=0,flag2=0;
int i=0;
flag1=solve(num1);
flag2=solve(num2);
// here we will simply check condition
if(flag1==1 and flag2==1)
{
cout<<"SWEET.";
}
if(flag1==1 and flag2==0)
{
cout<<"SOUR.";
}
if(flag1==0 and flag2==1)
{
cout<<"SALTY.";
}
if(flag1==0 and flag2==0)
{
cout<<"BITTER.";
}
return 0;
}
// This function will check a particular integer in perfect square or not
int is_perfect_squre(int n)
{
int i=0;
for(i=2;i*i<=n;i++)
{
if(i*i==n)
return 1;
}
return 0;
}
int solve(int num1)
{
int flag1=0,flag2=0;
int i=0;
vector sqr_factor; // To store the perfect square
// all factor of a number should be less then or equal to half of its value.
for(i=2;i<=num1/2;i++)
{
if(num1%i==0)
{
if(is_perfect_squre(i))// checking a number is perfect square or not
{
sqr_factor.push_back(i);//push into vector
}
}
}
if(is_perfect_squre(num1))
{
sqr_factor.push_back(num1);
}
if(sqr_factor.size() !=0)//if at least one perfect square factor exist then if condition will true.
{
cout<1) that are square: ";
for(i=0;i
{
cout<
}
cout<
return 0;
}
else
{
//no perfect square exist.
cout<1) that are square: (none)"<
return 1;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started