Make a program that calculates the value of H*S, being:

Asked

Viewed 134 times

-5

Problema em questão

I was using this solution based on a code from a friend: but we are uncertain about the outcome.

        double h = 0;
        double n = 4;
        
        double s = 0;
        double n1 = 36;
        
        for (int i = 1; i <=n; i++) {
            h = h +  (double) 1/i;
            
        }
        for (int i = 1; i <=n1; i++) {
            s = s - (double) 1/i;
        }
        
        double total;
        
        total = h*s;
            
            System.out.println("S: " + s);
            System.out.println("H: " + h);
            
            System.out.println("Valor total: " + total);

2 answers

1


Note that to calculate h and s you are always adding up (or subtracting) 1/i (that is, 1 divided by i). But note the definition that the numerator is not always 1, it also has to change. And in the case of s, the sign also changes at each term (an hour subtracts, then sums, subtracts, sums, etc).

So you have to control both the numerator and the denominator.

In the case of h, we have 50 terms: the denominator varies from 1 to 50, and the numerator increases by 2 by 2.
And in the s, are only 10 terms: the numerator varies from 1 to 10, and the denominator is the numerator squared. And we have to make the signal also alternate:

double h = 0.0;
double numerador = 1.0;
for (int i = 1; i <= 50; i++) {
    h += numerador / i;
    numerador += 2;
}
double s = 0.0;
double sinal = 1.0;
for (int i = 1; i <= 10; i++) {
    s += (sinal * i) / (i * i);
    sinal *= -1;
}
System.out.println("h=" + h);
System.out.println("s=" + s);
System.out.println("h * s=" + h * s);

1

The somatorios are wrong. Note that H = 1/1 + 3/2 + 5/3..., i.e., while the numerator of the fraction (the top one) starts at 1 and increases by 2, the denominator will increase by 1 in 1. Be aware that the numerator is a unit smaller than twice the denominator, this result is obtained thus (i * 2 - 1).

Note in defining the S problem that when the numerator is odd Voce must sum and when it is an even number Voce must subtract. Then check this condition with i % 2 == 0, if it is false then subtraia, because the numerator is even, otherwise some. in S the denominator is equal to square of the numerator Math.pow(i, 2).

import java.lang.Math;
public class Main {
  public static void main(String[] args) {
    double h = 0;
    double n = 50;
        
    double s = 0;
    double n1 = 10;
        
    for (int i = 1; i <= n; i ++) {
      h = h +  (double) (i * 2 - 1)/(i); 
    }

    for (int i = 1; i <=n1; i++) {
      if(i % 2 == 0){ 
        s = s - (double) i/Math.pow(i, 2);
      }
      else{
        s = s + (double) i/Math.pow(i, 2);
      }
    }
        
    double total;
        
    total = h*s;
            
    System.out.println("S: " + s);
    System.out.println("H: " + h);
            
    System.out.println("Valor total: " + total);  
  }
}
  • Thank you very much, I checked with my teacher, and he confirmed that it is a correct solution, I was breaking my head with this problem for quite some time, I had to resort to outside help. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.