I am new to c++ and started learning last month.I am trying to solve this problem:-
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
I wrote this program to take any number from user and return the smallest positive number that is evenly divisible by all of the numbers from 1 to that number.
The problem is the program is taking a lot of time to return the answer. Why?
The prime number function in this prog returns the nth prime number eg. if you pass the value 1, you get 2 as it the first prime number. If you pass 3, you get the third prime number ie 5. And the function smallest returns the answer to the whole problem of smallest positive numbers.
The whole problem seems that the primenumber function is too slow.When I wrote a program to print the first 100 prime number, it did display the first 9 - 10 primenumbers instantly, but for the next numbers were displayed after a gap of few seconds and and the gap got longer and longer as more prime numbers were displayed. I am with i3 and 4gb ram and dont expect this from a 40 line code.
What is the reason? How can I make my code more efficient?
The program:-
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
I wrote this program to take any number from user and return the smallest positive number that is evenly divisible by all of the numbers from 1 to that number.
The problem is the program is taking a lot of time to return the answer. Why?
The prime number function in this prog returns the nth prime number eg. if you pass the value 1, you get 2 as it the first prime number. If you pass 3, you get the third prime number ie 5. And the function smallest returns the answer to the whole problem of smallest positive numbers.
The whole problem seems that the primenumber function is too slow.When I wrote a program to print the first 100 prime number, it did display the first 9 - 10 primenumbers instantly, but for the next numbers were displayed after a gap of few seconds and and the gap got longer and longer as more prime numbers were displayed. I am with i3 and 4gb ram and dont expect this from a 40 line code.
What is the reason? How can I make my code more efficient?
The program:-
#include <iostream>
using namespace std;
int nAnswer = 1;
int primenumber(int x)
{
if(x==1)
return 2;
int iii = (primenumber(x-1)) + 1;
for(int n = (x-1);n >= 1;n--)
{
int z;
z = iii%(primenumber(x-n));
if(z==0)
{iii++;
n=x;}
}
return iii;
}
int smallest(int f)
{
for(int n=1;(primenumber(n))<=f;n++)
{nAnswer = nAnswer * (primenumber(n));}
for(int iii=2;int jjj=1;(primenumber(jjj)^iii)<=f)
{nAnswer = nAnswer * ((primenumber(jjj))^iii);
if (((primenumber(jjj))^(iii+1))<=f)
{iii++;
continue;}
else if(((primenumber(jjj+1))^2)<=f)
{iii=2;
jjj++;
continue;}
else
{break;}}
return nAnswer;}
int main()
{
int g;
cin>>g;
cout << smallest(g); //calling the function
return 0;
}
using namespace std;
int nAnswer = 1;
int primenumber(int x)
{
if(x==1)
return 2;
int iii = (primenumber(x-1)) + 1;
for(int n = (x-1);n >= 1;n--)
{
int z;
z = iii%(primenumber(x-n));
if(z==0)
{iii++;
n=x;}
}
return iii;
}
int smallest(int f)
{
for(int n=1;(primenumber(n))<=f;n++)
{nAnswer = nAnswer * (primenumber(n));}
for(int iii=2;int jjj=1;(primenumber(jjj)^iii)<=f)
{nAnswer = nAnswer * ((primenumber(jjj))^iii);
if (((primenumber(jjj))^(iii+1))<=f)
{iii++;
continue;}
else if(((primenumber(jjj+1))^2)<=f)
{iii=2;
jjj++;
continue;}
else
{break;}}
return nAnswer;}
int main()
{
int g;
cin>>g;
cout << smallest(g); //calling the function
return 0;
}