My Math Forum java programming incorrect answer

 Math Software Math Software - Mathematica, Matlab, Calculators, Graphing Software

 March 3rd, 2017, 06:50 AM #1 Newbie   Joined: Dec 2016 From: Netherlands Posts: 15 Thanks: 0 java programming incorrect answer i have to write a program that gives the summation of 1/i for any given i. i came up with the following program but for some reason it always provides the answer 1.0, can someone tell me what is wrong here? import java.util.Scanner; class Series{ Scanner sc = new Scanner(System.in); double series(int i){ if (i==1) return 1; else return 1/(i*i) + series(i-1); } public void demo(){ int i; System.out.println("Provide a number to compute the series for"); i = sc.nextInt(); System.out.println("series of " + i + " is " + series(i)); } public static void main(String[] args) { new Series().demo(); } }
March 3rd, 2017, 07:33 AM   #2
Math Team

Joined: Oct 2011

Posts: 11,057
Thanks: 721

Quote:
 Originally Posted by MathAboveMeth i have to write a program that gives the summation of 1/i for any given i.
Huh?
If i = 2, what d'hell is "summed"?
Your post is "obscure"...or do I need another coffee?

 March 3rd, 2017, 07:53 AM #3 Newbie     Joined: Feb 2017 From: Netherlands Posts: 8 Thanks: 2 Math Focus: Trigonometry and complex numbers Assuming $\displaystyle i$ is a variable and not the imaginary unit: $\displaystyle \sum_{i=0}^\infty \frac{\alpha}i = \infty \thinspace \text{if} \thinspace \alpha\ne{0} \thinspace \wedge \thinspace i\ne{0}$ This can be proven with the harmonic series test. So what do you mean exactly? If you mean just $\displaystyle \frac{1}i$ you can use $\displaystyle i^{-1}$
 March 3rd, 2017, 10:25 AM #4 Senior Member     Joined: Jul 2010 From: St. Augustine, FL., U.S.A.'s oldest city Posts: 12,155 Thanks: 463 Math Focus: Calculus/ODEs I believe the OP's program is supposed to find: $\displaystyle S_i=\sum_{k=1}^{i}\left(\frac{1}{k}\right)$ It looks to me though, the algorithm coded would provide: $\displaystyle S_i=\sum_{k=1}^{i}\left(\frac{1}{k^2}\right)$ However, I'm not a Java programmer.
 March 3rd, 2017, 10:46 AM #5 Member   Joined: Jan 2016 From: Athens, OH Posts: 61 Thanks: 36 Your only error is in method series. You said i is an int, so 1/i*i is 0 for any i>1! So just change 1/i*i to 1.0/i*i. By the way I am surprised that your program runs as given. I thought main had to be within a public class? Thanks from MarkFL

 Thread Tools Display Modes Linear Mode

 Similar Threads Thread Thread Starter Forum Replies Last Post mitch08 Computer Science 2 November 28th, 2015 06:51 AM CherryPi Calculus 56 February 2nd, 2012 02:13 AM duz Number Theory 7 February 9th, 2010 06:06 PM bob.dob Algebra 1 October 29th, 2008 01:50 PM conjecture Calculus 3 July 16th, 2008 06:12 PM

 Contact - Home - Forums - Cryptocurrency Forum - Top