-
Maths Programming Help
Hey, I new to C++ and I was trying to make a program that solved a maths problem for me (I wanted to find out how many times the anwer would be a positive integer, given that n is a positive integer):
n+17
------ // That's a dividing sign
n-7
I tried a few methods, but couldn't get what I want.
Here's my latest attempt:
Code:
#include <stdafx.h>
#include <iostream>
using namespace std;
float main()
{
float x, y, z;
for ( float n = 0; n < 1000; n = n + 14){
x = n - 7;
y = n + 17;
z = n / x;
if (z > 0){
cout<<y <<endl;
n = n - 17;
x = x - 7;
}
else {
cout<<y <<" is not a positive integer" <<endl;
}
}
cin.get();
return 0;
}
I'm using the Visual C++ Express 2005 program if tht helps. I thought I might make the variables floats to make sure the decimals aren't truncated in an int thinking the program will see them as positive integers anyway. Thanks!
-
If you want to know whether (n+17)/(n-7) is an integer, just check whether (n+17)%(n-7) == 0 using integer arithmetic. The % is the modulus (remainder) operator.
Edit: BTW, it's "int main()", not "float main()".
-
Thanks, I tried it once and it didn't work:
Code:
int main()
{
for (int n = 0; n < 1000; n = n + 14){
if ((n + 17)%(n - 7) == 0){
cout<<n <<endl;
}
}
cin.get();
return 0;
}
so then I assigned x the value of (n + 17)%(n - 7) here:
Code:
int main()
{
for (int n = 0; n < 1000; n = n + 14){
int x = (n + 17)%(n - 7);
if ( x == 0){
cout<<n <<endl;
}
}
cin.get();
return 0;
}
It still didn't work.
Note: There are very few answers to (n+17)/(n-7) which is why the computer might not be printing out anything.
-
On the one hand, you know that if n > 7, then (n+17)/(n-7) is greater than 1. On the other hand, as n increases, it decreases, and as n goes to infinity, it approaches 1, so for large enough n it will be less than 2 and so will never be an integer again. Keeping this in mind, you can probably work out by hand what all the possible values of n are.
-
> It still didn't work.
Have you tried
int n = someValueWhichIsKnownToWork;
int x = (n + 17)%(n - 7);
To check the code does actually produce the right answer given correct input?
-
Robatino, yeh I wanted to try and make a program that would find those for me, as I haven't learnt how to do it yet (at school). And in one of the different attempts the computer printed -10, and as the condition was set to < 10,000,000 in the for loop (I think) (I set it that high because I wanted to see if the numbers would ever come down to -1, before it was just 1000), it kept printing -10 then started printing -9 and so forth until -2, each time taking more loops to change the number, then started printing -1.9 and so forth until -1.1 taking even more time per number, and finally reached -1 (whole) (probably because the computer couldn't calculate any lower decimals).
Salem, as I said above, I wanted to find the answer with the help of a computer as I didn't know how to do it by hand.
Anyway thanks for your help, I'll just leave it unless someone knows maybe a differen way of doing this. Thanks again!
-
Code:
#include <iostream>
using namespace std;
int main() {
int n = 8;
while ((n+17)/(n-7) >= 2) {
if ((n+17)%(n-7) == 0) cout << n << "\n";
n++;
}
return 0;
}
Just a pretty simple loop if you do some basic math to determine where it will converge and where to start from. Obviously you don't want to start from 7 because you will get divide by 0, anything less will result in a negative number which isn't wanted. Keep looping until it's less than 2 because the fraction goes to 1 for inf, and anything between 1 and 2 can't be a positive integer. (Basically what everyone said above ;))
Btw, why did you increment n by 14 each time. The first "n" is actually 8 (25/1 = 25). Also when I ran it none were multiples of 14 - they were 8,9,10,11,13,15,19,31.
-
It's easier to do some of this by hand - for example, if (x+17)/(x-7) == 2, it's trivial to solve for x to get x == 31. So one can just check by hand (or in one's head) values of n between 8 and 30 (of course n == 31 is also a solution).
-
Stay away from floats and doubles if you need to work with real mathematical fractions.
You had the correct solution before, except you were doing n = n + 14
Code:
#include <iostream>
int main()
{
for (int n = 0; n < 1000; ++n){
// Careful to not divide by 0
if (n != 7 && (n + 17)%(n - 7) == 0){
std::cout<< n <<std::endl;
}
}
std::cin.get();
return 0;
}