My Math Forum  

Go Back   My Math Forum > Math Forums > Math Software

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


Thanks Tree1Thanks
  • 1 Post By johng40
Reply
 
LinkBack Thread Tools Display Modes
March 3rd, 2017, 05: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();
}
}
MathAboveMeth is offline  
 
March 3rd, 2017, 06:33 AM   #2
Math Team
 
Joined: Oct 2011
From: Ottawa Ontario, Canada

Posts: 10,098
Thanks: 683

Quote:
Originally Posted by MathAboveMeth View Post
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"?
Please provide an example.
Your post is "obscure"...or do I need another coffee?
Denis is offline  
March 3rd, 2017, 06:53 AM   #3
Newbie
 
Xxmarijnw's Avatar
 
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}$
Xxmarijnw is offline  
March 3rd, 2017, 09:25 AM   #4
Senior Member
 
MarkFL's Avatar
 
Joined: Jul 2010
From: St. Augustine, FL., U.S.A.'s oldest city

Posts: 12,155
Thanks: 462

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.
MarkFL is offline  
March 3rd, 2017, 09:46 AM   #5
Member
 
Joined: Jan 2016
From: Athens, OH

Posts: 45
Thanks: 26

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
johng40 is offline  
Reply

  My Math Forum > Math Forums > Math Software

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 05:51 AM
Is this incorrect? CherryPi Calculus 56 February 2nd, 2012 01:13 AM
A075441 has incorrect data duz Number Theory 7 February 9th, 2010 05:06 PM
probability of incorrect conclusion? confused.. bob.dob Algebra 1 October 29th, 2008 12:50 PM
two different answers in limits but one is incorrect conjecture Calculus 3 July 16th, 2008 05:12 PM





Copyright © 2017 My Math Forum. All rights reserved.