I'm having trouble trying to find the sum of this series non-recursively
So far I have:
public static double sum_nr(int n) {
int result = 0;
for (int i = 1; i<=n; i++)
{
result=result+1/(i+1);
}
return result;
}
public static void main(String[] args){
int n= 4;
System.out.println("Calculation for sum_nr(n) " + n + " is "+ sum_nr(n));
}
I keep getting 0.0 as the sum.
What am I doing wrong?
I think it's due to not using the right type. You're doing integer division rather than using a float type.
public static double sum_nr(int n) {
double result = 0;
for (double i = 0; i < n; i++)
{
result=result+1.0/(i+1);
}
return result;
}
Integer division will lead to 0 results. you should use float/double instead of int for variable result.
result=result+1/(i+1); will always give 0 as i is an integer. Assign i value to a float and use that
float result = 0f; // declare result as float too or double for (int
i = 1; i<=n; i++)
{ float val = i;
result=result+1/(float+1);
}
Same as everyone else says but their answers aren't complete.
public static double sum_nr(int n) {
float result = 0;
for (int i = 1; i<=n; i++){
result=result+1/((float)i+1);
}
return result;
}
result needs to be a float, or double, but you also need to cast i as a float, or double, as well. No need to create a new variable
The series you are referring to is the harmonic series. There's a closed form approximation for this.
H(n) = ln(n) + gamma + 1/(2n) - 1/(12n^2)
where gamma is the Euler-Mascheroni constant. See here.
In Java, here's some code to both compute correctly and show the difference of the approximation to the actual as n grows. It will become asymptotically smaller:
constant double EULER_MASCHERONI = 0.57721566490153286060651209008240243104215933593992;
public static double approximateHarmonicSummation(int n) {
return Math.log(n) + EULER_MASCHERONI + 0.5 * n - 1.0 / (12.0 * Math.pow(n, 2));
}
public static double sum_nr(int n) {
double result = 0;
for (double i = 0; i < n; i++) {
result += 1.0 / (i + 1);
}
return result;
}
public static void main(String[] args) {
double approx = 0.0;
double actual = 0.0;
for (int n = 0; n < 1000; n++) {
approx = approximateHarmonicSummation(n);
actual = sum_nr(n);
System.out.println("Calculation for approximation of sum_nr(n) " + n + " is "+ approx);
System.out.println("Calculation of sum_nr(n) " + n + " is "+ actual);
System.out.printlin("Difference = " + (actual - approx) + "\n");
}
}
Hopefully that helps.
Related
i'm trying to build an data structures and algorithms program using java.
and i wanna ask if anyone know
How do i calculate the percentage increase or decrease in a array.
i try this code--but apparently its not the correct way
public class test{
static float checkMov(int arr[], int n) {
float var1, var2, result = 0;
var1 = arr[0];
var2 = arr[1];
// Calculate successive change of 1st 2 change
result = var1 + var2 + ((var1 * var2) / 100);
// Calculate successive change
// for rest of the value
for (int i = 2; i < n; i++) {
result = result + arr[i] + ((result * arr[i]) / 100);
}
return result;
}
}
import static test.testt.checkMov;
public class tester {
public static void main(String[] args)
int[] arr = {1931534,1933628,1935714,1925709,1923754,1923578,1923049,1928556,1928289,1924857};
int N = arr.length;
// Calling function
float result = checkMov(arr, N);
System.out.println("Percentage change is = " + result + " %");
}
}
Run->
Percentage change is = Infinity %
Here you're dealing with the numbers, which could go in the larger values. Datatype float has limited range to handle the numbers between 3.40282347 x 10e38 and 1.40239846 x 10e-45. So change the method return type of checkMov to double. Also handle the values in double, which ranges between 1.7976931348623157 x 10e308 and 4.9406564584124654 x 10e-324.
static double checkMov(int arr[], int n) {
double var1, var2, result = 0;
var1 = arr[0];
var2 = arr[1];
// Calculate successive change of 1st 2 change
result = var1 + var2 + ((var1 * var2) / 100);
// Calculate successive change
// for rest of the value
for (int i = 2; i < n; i++) {
result = result + arr[i] + ((result * arr[i]) / 100);
}
return result; // This returns 7.0955315970304415E44
}
I need help figuring out the following problem :
Create a class Statistics.java to implement the StatisticsI.java. Statistics.java contains ArrayList data to store to data objects of Double type, and properties (attributes): count, min, max, mean, std. The implementation has the following specifications.
addData(Double d) adds data d into the array list, and then update the values of count, min, max, mean, and std, using incremental algorithms, i.e. using exiting existing value to calculate new values, rather than calculating by traversal through the data array.
getCount() returns count.
getMin() returns min.
getMax() returns max.
getMean() returns mean.
getSTD() returns std.
stats() computes the count, min, max, mean, and stddev from the data array in one loop, and then sets the values of the properties.
I have the interface
public interface StatisticsI {
void addData(double d);
int getCount();
double getMin();
double getMax();
double getMean();
double getSTD();
void stats();
}
Here is the code I have so far for the implementation part.
import java.util.ArrayList;
public class Statistics implements StatisticsI {
private ArrayList<Double> data;
private long count;
private double min;
private double max;
private double mean;
private double std;
public Statistics() {
data = new ArrayList<Double>();
count = 0;
min = 0;
max = 0;
mean = 0;
std = 0;
}
public void addData(double d) {
data.add(d);
count += 1;
data.set(data.indexOf(min), min);
data.set(data.indexOf(max), max);
data.set(data.indexOf(mean), mean);
data.set(data.indexOf(std), std);
}
public int getCount() {
count = data.size();
return (int) count;
}
public double getMin() {
return min;
}
public double getMax() {
return max;
}
public double getMean() {
int mean = 0;
for (int i = 0; i < data.size(); i++) {
double currentNum = data.get(i);
mean += currentNum;
}
return mean / data.size();
}
public double getSTD() {
{
double avg = getMean();
double t = 0;
for (int i = 0; i < data.size(); i++) {
double numbers = data.get(i);
double value = Math.pow(numbers - avg, 2);
t += value;
}
double std = (double) t / (double) (data.size());
return Math.sqrt(std);
}
}
public void stats() {
count = data.size();
}
}
I am having trouble with the addData and stats methods and I am not sure if I am doing this correctly and I am trying to figure out how to implement it.
My question is how to implement the interface with what is required in the methods.
Update: How do I call the methods from Statistics
Here is the code for the main class
public static void main(String[] args) {
ArrayList<Double> numList = new ArrayList<Double>();
StatisticsI stats = new Statistics();
Random r = new Random();
for(int i = 1; i <= 10; i++) {
numList.add((double) r.nextInt(10000));
}
double count = stats.getCount();
double min = stats.getMin();
double max = stats.getMax();
double mean = stats.getMean();
double std = stats.getSTD();
System.out.println("The count for the list is:"+ count);
System.out.println("The min for the list is:" + min);
System.out.println("The max for the list is:" + max);
System.out.println("The mean for the list is:" + mean);
System.out.println("The standard deviation for the list is:" + std);
}
}
The problem is I don't get any values for my output, I get :
The count for the list is:0.0
The min for the list is:Infinity
The max for the list is:-Infinity
The mean for the list is:0.0
The standard deviation for the list is:NaN
How do I get my output to work?
in addData(..), use "min = Math.min(min, d)". Math.max also exists, but std and mean are trickier. It's probably easiest to calculate those in the getters by iterating the list.
You can update min, max, mean values immediately in addData.
Update It is also possible to use Welford's algorithm to calculate running deviation.
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
double variance = 0.0;
public void addData(double d) {
data.add(d);
count++;
if (d < min) {
min = d;
} else if (d > max) {
max = d;
}
// fixed calculation of running mean and variance by Welford's algorithm
double new_mean = mean + (d - mean) / count;
variance += count < 2 ? 0.0 : (d - mean) * (d - new_mean);
mean = new_mean;
std = count > 1 ? Math.sqrt(variance / (count - 1)) : 0;
}
Or, you can reset all these values in stats() method using the data list.
There is a convenient way to calculate all stats at once via DoubleSummaryStatistics and Java 8 Stream API:
public void stats() {
DoubleSummaryStatistics stats = data.stream().collect(Collectors.summarizingDouble(x -> x));
count = stats.getCount(); // long value returned instead of `int data.size()`
min = stats.getMin();
max = stats.getMax();
mean = stats.getAverage();
std = this.getSTD();
}
public double getSTD() {
double avg = getMean();
double t = 0.0;
for (double d : data) {
t += Math.pow(avg - d, 2);
}
return Math.sqrt(t / getCount());
}
Loop-based "streamless" calculation of stats including "naive" variance calculation algorithm for std could be as follows:
public void stats() {
count = data.size();
min = Double.POSITIVE_INFINITY;
max = Double.NEGATIVE_INFINITY;
mean = 0.0;
variance = 0.0;
double sum = 0.0;
double sq = 0.0;
int i = 0;
for (double d : data) {
min = Math.min(min, d);
max = Math.max(min, d);
i++;
// calculate running mean as above to use in variance
double new_mean = mean + (d - mean) / i;
variance += i < 2 ? 0 : (d - mean) * (d - new_mean);
mean = new_mean;
sum += d;
sq += d * d;
}
System.out.println("running mean=" + mean);
std = count > 1 ? Math.sqrt((sq - mean * mean / count) / (count - 1)) : 0;
mean = count == 0 ? 0 : sum / count;
}
// override STD getter to remove all calculations
public double getSTD() {
return std;
}
Test
Statistics stats = new Statistics();
stats.addData(17);
stats.addData(19);
stats.addData(24);
System.out.println("after addData()");
System.out.println("mean=" + stats.getMean());
System.out.println("std=" + stats.getSTD());
System.out.println("after addData()");
stats.stats();
System.out.println("mean=" + stats.getMean());
System.out.println("std=" + stats.getSTD());
Output
after addData()
mean=20.0
std=3.605551275463989
after stats()
running mean=20.0
mean=20.0
std=3.605551275463989
I am trying to use java to pass an array to get the mean, median,mode , max an min in java. I am currently having an issue passing the array to a function and return its value so i can output the results. I believe i have the loops correct to solve the mean median and mode but i cannot get them to send and receive as wanted. How can I pass the array and send back the values needed?
UPDATE: i have updated the code it will compile and i can input the number of years but i get several errors following after that. it is also not printing the outputs
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'i'
at java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2646)
at java.util.Formatter$FormatSpecifier.(Formatter.java:2675)
at java.util.Formatter.parse(Formatter.java:2528)
at java.util.Formatter.format(Formatter.java:2469)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at la5cs1110_woodspl_03.pkg17.pkg2016.La5cs1110_WoodsPl_03172016.main(La5cs1110_WoodsPl_03172016.java:56)
Java Result: 1
public static void main(String[] args) {
int i;
List<Double> hArray = new ArrayList<>();
int nYears = 0, y = 0;
double rMax = 0.00,rMin = 100.00;
//get input check if between 1-80
while(y == 0){
String userData = JOptionPane.showInputDialog
("Enter number of years");
nYears = Integer.parseInt(userData);
if (nYears > 1 && nYears <= 80 )
y = 1;
}
y = 0;
while(y <= nYears){
for(i = 0; i < 12; i++){
Random rand = new Random();
double rNum = rand.nextFloat() * (rMax - rMin) + rMin;
hArray.add(rNum);
}
double mean = getMean (hArray);
double median = getMedian (hArray);
double mode = getMode (hArray);
double max = getMaxValue(hArray);
double min = getMinValue (hArray);
System.out.printf("In year %i the Mean = %d , mode = %d, median = %d," +
" max = %d, min = %d", y , mean, median, mode, max, min);
y++;
}
}
private static double getMean(List<Double> hArray) {
double sum = 0;
for (int i = 0; i < hArray.size(); i++) {
sum += hArray.get(i);
}
return sum / hArray.size();
}
//Median
private static double getMedian(List<Double> hArray) {
int middle = hArray.size()/2;
if (hArray.size() %2 == 1) {
return hArray.get(middle);
} else {
return (hArray.get(middle-1) + hArray.get(middle)) / 2.0;
}
}
//Mode
public static double getMode(List<Double> hArray) {
double maxValue = 0, maxCount = 0;
for (int i = 0; i < hArray.size(); ++i) {
int count = 0;
for (int j = 0; j < hArray.size(); ++j) {
if (hArray.get(j) == hArray.get(i)) ++count;
}
if (count > maxCount) {
maxCount = count;
maxValue = hArray.get(i);
}
}
return maxValue;
}
public static double getMaxValue(List<Double> hArray){
double maxValue = hArray.get(0);
for(int i=1;i < hArray.size();i++){
if(hArray.get(i) > maxValue){
maxValue = hArray.get(i);
}
}
return maxValue;
}
public static double getMinValue(List<Double> hArray){
double minValue = hArray.get(0);
for(int i=1;i<hArray.size();i++){
if(hArray.get(i) < minValue){
minValue = hArray.get(i);
}
}
return minValue;
}
}
Your hArray is a List. You should convert it to an array first.
getMean(hArray.toArray)
Check out this.
This does not compile, you try to pass a Double to a method, which expects a double[]. So you have to change the parameter of your methods and use a List and just pass in the hArray (see Tibrogargan answer - i.e., you would have to modify each of your implementations) or do the following:
create a Double[]
Double[] hArray2 = hArray.toArray(new Double[hArray.size()]);
change your methods' signature, so that they expect an Double[]
private static double getMean(Double[] hArray) { ...}
pass hArray2 instead of hArray
double mean = getMean(hArray2);
// ...
That should be it.
Replace the section where you're trying to pass a single element from the array to your statistics functions with calls using the whole array and change the signature of the calls so they take a List<Double> param, not a double[]. Something like this:
double mean = getMean (hArray);
double median = getMedian (hArray);
double mode = getMode (hArray);
double max = getMaxValue(hArray);
double min = getMinValue (hArray);
//Mean
private static double getMean(List<Double> hArray) {
double sum = 0;
for (int i = 0; i < hArray.size(); i++) {
sum += hArray.get(i);
}
return sum / hArray.size();
}
See also: How do you calculate the variance, median, and standard deviation in C++ or Java?
Fix for median:
Copied directly from this above link with some minor modifications to use a List as a param
public Double median(List<Double> list)
{
Double[] array = list.toArray(new Double[list.size()]);
Arrays.sort(data);
if (data.length % 2 == 0)
{
return (data[(data.length / 2) - 1] + data[data.length / 2]) / 2.0;
}
else
{
return data[data.length / 2];
}
}
Fix for mode:
public Double mode(List<Double> list)
{
java.util.TreeMap<Double,Integer> map = new java.util.TreeMap<>();
Double maxVal = null;
int maxCount = 0;
for (Double d : list) {
int count = 0;
if (map.containsKey(d)) {
count = map.get(d) + 1;
} else {
count = 1;
}
map.put(d, count);
if (count > maxCount) {
maxVal = d;
maxCount = count;
}
}
return maxVal;
}
I am trying to find out the sum of the series, 1/2! - 2/3! + 3/4! - 4/5! ... n. Sorry if this sounds awkward but the sum always shows up as 0.0. I can't figure out what's happening and I am just starting out. Can anyone kindly point out the mistake and suggest how to fix it? Thanks!
import java.util.Scanner;
public class Series {
/*
* Series design: 1/2! - 2/3! + 3/4! - 4/5! .. n
*/
static double sum = 0; static int n;
Scanner sc = new Scanner(System.in);
public static int fact(int n){
int fact = 1;
for (int i = 1; i<=n; i++){
fact *= i;
}
return fact;
}
void generate(){
double sign = 1.0; double term;
for (int i = 1; i<=n; i++){
term = i/fact(i+1) * sign;
sum += term;
sign *= -1;
}
}
void accept(){
System.out.println("Enter the value of n:");
n = sc.nextInt();
}
public static void main(String[] args){
Series o = new Series();
o.accept();
o.generate();
System.out.println("The sum of the series is: " +sum);
}
}
i and fact(i+1) are both ints, so you're performing integer division. Since i < fact(i+1), each such term will produce a zero.
You had the right idea with defining sign as a double, but since / and * have the same precedence, you're first performing an integer division and only then multiplying by the double sign. Moving it to the beginning of the expression should do the trick:
void generate(){
double sign = 1.0; double term;
for (int i = 1; i<=n; i++){
term = (sign * i) / fact(i+1);
sum += term;
sign *= -1;
}
}
Your problem is that i/fact(i+1) is an int division, so it is truncated to 0 (since it's smaller than 1).
Change it to (double)i/fact(i+1).
Alternately, you can write
term = sign*i/fact(i+1);
since sign is already double, so it would ensure sign*i would also be double, and the division would be a floating point division.
import java.util.Scanner;
public class Series {
static double sum = 0.0;
static int n=0;
static Scanner sc = new Scanner(System.in);
public static int fact(int n)
{
int fact = 1;
for(int i = 1; i<=n; i++){
fact *= i;
}
return fact;
}
static void generate(){
//because of static property
double sign = 1.0; double term;
for(int i = 1; i<=n; i++){
term = (i* sign)/fact(i+1) ;
sum += term;
sign *= -1;
}
}
static void accept(){
//because of static property
System.out.println("Enter the value of n:");
n = sc.nextInt();
}
public static void main(String[] args){
Series.accept();
Series.generate();
System.out.println("The sum of the series is: " +sum);
}
}
The question here would be to get the sum of powers (m^0 + m^1 + m^2 + m^3.... + m^n) using only FOR loops. Meaning, not using any other loops as well as Math.pow();
Is it even possible? So far, I am only able to work around getting m^n, but not the rest.
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
int total = 1;
System.out.print("Enter value of m: ");
int m = scn.nextInt();
System.out.print("Enter value of n: ");
int n = scn.nextInt();
for (int i = 1; i <= n; i++){
total * m;
}
System.out.print(total);
}
Let's say m =8; and n = 4;
i gives me '1,2,3,4' which is what I need, but I am unable to power m ^ i.
Would be nice if someone could guide me into how it could be done, can't seem to progress onwards as I have limited knowledge in Java.
Thanks in advance!
You might want to rewrite it like this :
m^0 + m^1 + m^2 + m^3.... + m^n = 1 + m * (1 + m * (1 + m * (.... ) ) )
And you do it in a single for loop.
This should do the job (see explanations in comments):
public long count(long m, int pow) {
long result = 1;
for(int i = 0;i<pow; i++) {
result*=m +1;
}
return result;
}
You can nest loops. Use one to compute the powers and another to sum them.
You can do below:
int mul = 1;
total = 1;
for(int i=1;i<=n;i++) {
mul *= m;
total += mul;
}
System.out.println(total);
You can use a single loop which is O(N) instead of nested loops which is O(N^2)
long total = 1, power = m
for (int i = 1; i <= n; i++){
total += power;
power *= m;
}
System.out.print(total);
You can also use the formula for geometric series:
Sum[i = k..k+n](a^i) = (a^k - a^(k+n+1)) / (1 - a)
= a^k * (1 - a^(n+1)) / (1 - a)
With this, the implementation can be done in a single for loop (or 2 simple for loop): either with O(n) simple looping, or with O(log n) exponentiation by squaring.
However, the drawback is that the data type must be able to hold at least (1 - a^(n+1)), while summing up normally only requires the result to fit in the data type.
This is the solution :
for(int i=0;i<n;i++){
temp=1;
for(int j=0;j<=i;j++){
temp *= m;
}
total += temp;
}
System.out.println(total+1);
You can easily calculate powers using your own pow function, something like:
private static long power(int a, int b) {
if (b < 0) {
throw new UnsupportedOperationException("Negative powers not supported.");
}
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
return a * power(a, b - 1);
}
Then simply loop over all the values and add them up:
long out = 0;
for (int i = 0; i <= n; ++i) {
out += power(m, i);
}
System.out.println(out);
I would add that this is a classic dynamic programming problem as m^n is m * m^(n-1). I would therefore add caching of previously calculated powers so that you don't have to recalculate.
private static Map<Integer, Long> powers;
public static void main(String args[]) {
int m = 4;
int n = 4;
powers = new HashMap<>();
long out = 0;
for (int i = 0; i <= n; ++i) {
out += power(m, i);
}
System.out.println(out);
System.out.println(powers);
}
private static long power(int a, int b) {
if (b < 0) {
throw new UnsupportedOperationException("Negative powers not supported.");
}
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
Long power = powers.get(b);
if (power == null) {
power = a * power(a, b - 1);
powers.put(b, power);
}
return power;
}
This caches calculated values so that you only calculate the next multiple each time.