This program will output integers from m to n (user inputs) that are not divisible by a perfect square except 1.
I just want to ask if there is a further simplification for this algorithm.
//square-free number
#include<iostream.h>
void main()
{
long int x, z, count, m, n;
cout<<"Output square free integers from m to n: ";
cin>>m>>n;
x=m-1;
count=0;
do
{
count++;
x++;
z=1;
while (z<=x/2)
{
z++;
if (x%(z*z)==0)
z=x+3;
}
if (z!=x+3)
cout<<count<<": "<<x<<endl;
}while (x<n);
}
There are certainly more efficient algorithms than either of these, but I'm not sure if by "simplification" that OP is asking for more efficient algorithms or more easily understood ones.
Let [ m...n ] define a range of non-negative integers, m <= n.
Take the square root of m and truncate the decimal.
Let this value be k.
while( k*k >= m && k*k <= n ) {
output k;
increment k;
}
This algorithm is O(n) requiring 1 sqrt, (m-n) multiplies, and 2(m-n)
comparisons.
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
square-free_number.cpp:
Error E2265 square-free_number.cpp 1: No file name ending
Error E2060 square-free_number.cpp 19: Illegal use of floating point in function
main()
Error E2188 square-free_number.cpp 19: Expression syntax in function main()
Error E2379 square-free_number.cpp 27: Statement missing ; in function main()
Error E2379 square-free_number.cpp 34: Statement missing ; in function main()
*** 5 errors in Compile *** that's for computer quips program. also, there is an error in my code. the count must be the ordinality of the output square free number. help me fix this code