
Math Software Math Software  Mathematica, Matlab, Calculators, Graphing Software 
 LinkBack  Thread Tools  Display Modes 
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(i1); } 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 From: Ottawa Ontario, Canada Posts: 11,057 Thanks: 721  
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? 

Tags 
answer, incorrect, java, programming 
Thread Tools  
Display Modes  

Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Question regarding python programming, specifically Object Oriented Programming  mitch08  Computer Science  2  November 28th, 2015 06:51 AM 
Is this incorrect?  CherryPi  Calculus  56  February 2nd, 2012 02:13 AM 
A075441 has incorrect data  duz  Number Theory  7  February 9th, 2010 06:06 PM 
probability of incorrect conclusion? confused..  bob.dob  Algebra  1  October 29th, 2008 01:50 PM 
two different answers in limits but one is incorrect  conjecture  Calculus  3  July 16th, 2008 06:12 PM 