Incrementing fractions based on user input?

Page 1 of 1

11 Replies - 982 Views - Last Post: 23 October 2011 - 03:22 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=252367&amp;s=c424d5cc2dd290ad732fe007db795d7c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 HapticThreek

Reputation: 0
• 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

Reputation: 14
• 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
}

```

#3 pbl

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

Reputation: 8379
• 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

#4 HapticThreek

Reputation: 0
• 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.

#5 I ♣ Seals

Reputation: 14
• 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).

#6 HapticThreek

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

Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 03:52 PM

I ♣ 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

#7 HapticThreek

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

Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 04:18 PM

pbl, 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

I ♣ 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 :-)

#8 pbl

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

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

Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 06:08 PM

HapticThreek, 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

#9 HapticThreek

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

Re: Incrementing fractions based on user input?

Posted 22 October 2011 - 01:12 PM

I ♣ 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).

pbl, on 21 October 2011 - 06:08 PM, said:

HapticThreek, 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)
{
int input = KBInput.readInt(), numerat = 4, denom = 3;
System.out.println("Pi will be calculated over " + input + " terms.");

if (input == 0)
{
System.out.println("Pi over " + input + " terms is " + answer + ".");
System.out.println("Try a number above zero please!");
}
else if (input == 1)
{
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

#10 I ♣ Seals

Reputation: 14
• 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

#11 pbl

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

Reputation: 8379
• 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;
}
}

```

#12 HapticThreek

Reputation: 0
• 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.