Thread: Maths Programming Help

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    4

    Question 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!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If you want to know whether (n+17)/(n-7) is an integer, just check whether (n+17)&#37;(n-7) == 0 using integer arithmetic. The % is the modulus (remainder) operator.

    Edit: BTW, it's "int main()", not "float main()".
    Last edited by robatino; 04-20-2007 at 09:19 PM.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    4
    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.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > It still didn't work.
    Have you tried

    int n = someValueWhichIsKnownToWork;
    int x = (n + 17)&#37;(n - 7);

    To check the code does actually produce the right answer given correct input?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    4
    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!
    Last edited by UPNPAD; 04-21-2007 at 12:14 AM. Reason: Just a spelling mistake

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    	int n = 8;
    	while ((n+17)/(n-7) >= 2) {
    		if ((n+17)&#37;(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.
    Last edited by 0rion; 04-23-2007 at 03:19 AM.
    The cost of software maintenance increases with the square of the programmer's creativity.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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).
    Last edited by robatino; 04-23-2007 at 07:14 AM.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    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)&#37;(n - 7) == 0){
             std::cout<< n <<std::endl;
    		}
    	}
       std::cin.get();
    	return 0;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maths
    By AcerN30 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-03-2008, 01:13 PM
  2. maths???
    By nerdyneo in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2003, 01:04 PM
  3. Is maths REALLY required for programming?
    By FloatingPoint in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 07-12-2003, 01:18 PM
  4. maths in a program
    By anthonye in forum C Programming
    Replies: 2
    Last Post: 05-24-2002, 09:08 AM
  5. More Maths :(
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-20-2002, 10:39 AM