Hi i am writing a program to compute the smallest number divisible by each of the numbers 1 to 20. This is what i have so far but it does not cout anything. it just gives me a blank page. Could anyone please help

``````#include<iostream.h>
#include<math.h>

bool div(ull y)
{
for(int x=1;x<=20;x++)
if(y%x!=0)
return false;
return true;
}
int main()
{
for(int x=1;;x++)
if(div(x)){
cout << x << endl;
system("pause");
}
}``````
3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by StuXYZ

You need to declare x as an int like this: int x; or int x =(INPUT NUMBER HERE)
That should work. If not tell me.

Id also try: return 0;

Thanks i tried that and this is what it gave me.

``````#include<iostream.h>
#include<math.h>

bool div(ull x) // syntax error here and ull was not declared
int x;
{ // syntax error before '{'token
for(int i=1;i<=20;i++) // syntax error before '<=' token and before '++" token
if(x%i!=0)
return false;
return true;
}
int main()
{
for(int i=1;;i++)
if(div(i)){
cout << i << endl;
}
system("pause");

}``````

This kind of question is designed to show you that a loop for all the numbers N=1 to N=LOTS [because the answer is big!], is not the way! Even if you got your div function you work, you could easily be spending a long time waiting for it to work.

[Note to teachers: don't set that as 1-20 since that is far too quick on todays hardware but 1-25 would be a better set, just doable in typical long int sizes [64bit] but not in int, and takes sufficient long but not stupidly long time that someone doing a brute force approach will not be able to do it]

To give you a large hint, a very similar question on a 1970's maths paper I saw as a kid. [Sorry I am old!!!]. Note that was before electronic calculators!! You need to think about primes and prime factors. For example, since the solution must divide by 7, and as 7 is prime, you only need to consider multiples of 7.

From that you should be able to figure out how to do it.

p.s. I have no idea what ull means, just delete it. You seem to have grabbed a C function from somewhere and dropped it into your C++ program.

Edited by StuXYZ: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.