11 Replies - 970 Views - Last Post: 23 October 2011 - 03:22 PM Rate Topic: -----

#1 HapticThreek   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 20-October 11

Incrementing fractions based on user input?

Posted 21 October 2011 - 03:03 PM

Hullo folks,
I'm having a bit of a hard time figuring out how to make a program that works out pi using the infinite sequence based on user input.
e.g.

User inputs "5",
Program works out that pi = 4 - 4/3 + 4/5 - 4/7 + 4/9

User inputs "8",
Program works out that pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13 - 4/15
etc.

Basically I have no clue how to make the input convert to number of terms without coding all posibilities (obviously not practical). Does anyone have any pointers? I hope I'm not just being dense :)
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Incrementing fractions based on user input?

#2 I ♣ Seals   User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 52
  • Joined: 20-November 10

Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 03:10 PM

Use a for loop.
int userInput;
//get a value from the user
for (int i = 0; i < userInput; i++) {
    //do whatever you need to do here
}


Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 03:13 PM

What have you tried ?
I guess it is a simple for loop from 1 to limit input by user and the vakue starts at 4
depending of i % 2 the operation is + or -
the numerator is always 4
the denomitor starts at 3 and is always denominator += 2 for the next iteration
Was This Post Helpful? 1
  • +
  • -

#4 HapticThreek   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 20-October 11

Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 03:30 PM

Seals yeah I assumed it was a loop of some sort but as pbl says its only the denominator that increases and by 2 not just 1.

pbl, I haven't actually started it yet, I'm just trying to wrap my head around how I'd even start this :-(
I think the main problem I'm having is the fact that the user could input 4234723482 or whatever and it would have to work out pi to that number of terms.
Was This Post Helpful? 0
  • +
  • -

#5 I ♣ Seals   User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 52
  • Joined: 20-November 10

Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 03:39 PM

Your fraction is still expressable in terms of i for each iteration. Think 3.0 + 2*(i-1).
Was This Post Helpful? 1
  • +
  • -

#6 HapticThreek   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 20-October 11

Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 03:52 PM

View PostI ♣ Seals, on 21 October 2011 - 03:39 PM, said:

Your fraction is still expressable in terms of i for each iteration. Think 3.0 + 2*(i-1).


Aahh I think I understand your for loop now! Let me try writing something
Was This Post Helpful? 0
  • +
  • -

#7 HapticThreek   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 20-October 11

Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 04:18 PM

View Postpbl, on 21 October 2011 - 03:13 PM, said:

What have you tried ?
I guess it is a simple for loop from 1 to limit input by user and the vakue starts at 4
depending of i % 2 the operation is + or -
the numerator is always 4
the denomitor starts at 3 and is always denominator += 2 for the next iteration



View PostI ♣ Seals, on 21 October 2011 - 03:39 PM, said:

Your fraction is still expressable in terms of i for each iteration. Think 3.0 + 2*(i-1).


I'm going to sleep on this and try again tomorrow, thank you both for your help, I will post again tomorrow with how I get along :-)
Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 06:08 PM

View PostHapticThreek, on 21 October 2011 - 06:30 PM, said:

pbl, I haven't actually started it yet, I'm just trying to wrap my head around how I'd even start this :-(
I think the main problem I'm having is the fact that the user could input 4234723482 or whatever and it would have to work out pi to that number of terms.

Still sign based on iteration index +/- based on index % 2
Still denominator += 2
Was This Post Helpful? 1
  • +
  • -

#9 HapticThreek   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 20-October 11

Re: Incrementing fractions based on user input?

Posted 22 October 2011 - 01:12 PM

View PostI ♣ Seals, on 21 October 2011 - 03:39 PM, said:

Your fraction is still expressable in terms of i for each iteration. Think 3.0 + 2*(i-1).



View Postpbl, on 21 October 2011 - 06:08 PM, said:

View PostHapticThreek, on 21 October 2011 - 06:30 PM, said:

pbl, I haven't actually started it yet, I'm just trying to wrap my head around how I'd even start this :-(
I think the main problem I'm having is the fact that the user could input 4234723482 or whatever and it would have to work out pi to that number of terms.

Still sign based on iteration index +/- based on index % 2
Still denominator += 2


OK. Here is what I have (I realise that I haven't done the main bit.
public class Pi
{
	public static void main (String [] args)
	{
		System.out.println("Please input number of terms");
		double pi = 4.0, answer;
		int input = KBInput.readInt(), numerat = 4, denom = 3;
		System.out.println("Pi will be calculated over " + input + " terms.");
		
		if (input == 0)
		{
			answer = 0;
			System.out.println("Pi over " + input + " terms is " + answer + ".");
			System.out.println("Try a number above zero please!");
		}
		else if (input == 1)
		{
		   answer = pi;
			System.out.println("Pi over " + input + " terms is " + answer + ".");
		}
		else
		{
			for (int i = 0; i < input; i++)
			{
			// ARRRRGHHH!
			}
		}
	}
}


I know that if i % 2 is 0 then the next operator will be - and if its 1 then + will be the operator. I also know that the final denominator will be (input * 2 - 1). I'm guessing that +=2 means it goes up by 2 each time starting at 3, but I dont know how to code this in. I'm sorry I'm at a block here! And I feel as if I'm being a bit of a block head too! heh, appreciate your help
Was This Post Helpful? 0
  • +
  • -

#10 I ♣ Seals   User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 52
  • Joined: 20-November 10

Re: Incrementing fractions based on user input?

Posted 23 October 2011 - 08:52 AM

Firstly, since 0 is a special case, you'd be best off ignoring it entirely and having your loop start from 1.

double pi = 4.0;
for (int i = 1; i < input; i++) {
    // The following line deals with the sign
    double numerator = (i % 2 == 0) ? 4.0 : -4.0;
    // I'll leave the rest for you to complete
}


This post has been edited by I ♣ Seals: 23 October 2011 - 08:53 AM

Was This Post Helpful? 1
  • +
  • -

#11 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Incrementing fractions based on user input?

Posted 23 October 2011 - 02:01 PM

Actually it is cute, and it works
after 10000 iterations I have
3.1414926535900345
3.1416926435905346

	public static void main(String[] args) {
		double x = 4.0;
		int nbIter = 10000;
		double denom = 3.0;
		double sign = -1.0;
		
		for(int i = 0; i < nbIter; ++i) {
			double elem = 4.0 / denom;
			x = x + (sign * elem);
			System.out.println(x);
			denom += 2.0;
			sign = sign * -1.0;
		}
	}


Was This Post Helpful? 1
  • +
  • -

#12 HapticThreek   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 20-October 11

Re: Incrementing fractions based on user input?

Posted 23 October 2011 - 03:22 PM

Thank you for both of your help, I really appreciate it. I shall hunker down to work and get it done! Thanks for helping me understand the problem.

:^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1