Square free number program

closed account (jNU5fSEw)
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);
}
closed account (S6k9GNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cmath.h)
#include <iostream.h>

void main()
{

  bool bDivisible;
  long double m, n;
  int i;
  int j;

  cout << "Output square free integers from m to n: ";
  cin >> m >> n;

  for (i = m; i <= n; ++i)
  {
     for (j = 2; j < i; ++j)
     {
       if ( sqrt(j) % 1) == 0 )
       {
         if ( (i / j) % 1 != 0)
         {
           bDivisible = False;
           break;
         } 
       }
     }
     if (bDivisible)
     {
       cout << m << endl;
     }
  }
  getchar()
}


:/ This is all I came up with.
Last edited on
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.



closed account (jNU5fSEw)
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
Topic archived. No new replies allowed.