简体   繁体   English

使用Java交替阶乘项

[英]Alternating factorial terms using Java

I'm trying to write a loop in Java that can output the sum of a series which has this form... 1! 我正在尝试用Java编写一个循环,该循环可以输出具有这种形式的序列的总和... 1! -3! -3! + 5! + 5! – 7! – 7! + ... up to n (user gives n as a positive odd number). + ...最多n个(用户将n表示为正奇数)。 For example, if the user inputs 5 for n, then the series should calculate the sum of 1! 例如,如果用户为n输入5,则该系列应计算1的总和! -3! -3! + 5! + 5! (hard part) & display it to the user with a basic print statement (easy part). (困难的部分)并通过基本的打印语句(简单的部分)向用户显示。 If the user gives 9, then the sum calculated would come from 1! 如果用户给出9,则计算得出的总和将为1! -3! -3! + 5! + 5! - 7! -7! + 9!. + 9!

For ease, just assume the user always puts in a positive odd number at any time. 为简便起见,假设用户始终在任何时候都输入正奇数。 I'm just concerned about trying to make a sum using a loop for now. 我只是担心现在尝试使用循环来求和。

The closest code I've come up with to do this... 我想出的最接近的代码来做到这一点...

int counter = 1;
int prod = 1;   
n = console.nextInt(); 
while (counter <= n) 
{                  
    prod = prod * counter;
    counter++; 
}   
System.out.println(prod);

This does n!, but I'm finding it hard to get it do as specified. 这确实是n !,但是我发现很难按规定执行它。 Any pointers would be great. 任何指针都很棒。

As you calculate the factorials, keep a running total of the series so far. 在计算阶乘时,请保持到目前为止该系列的运行总计。 Whenever counter % 4 == 1 , add the factorial to the running total. 每当counter % 4 == 1 ,将阶乘添加到运行总计中。 Whenever counter % 4 == 3 , subtract the factorial from the running total. 每当counter % 4 == 3 ,从运行总计中减去阶乘。

You said "any pointers" - I assume that means you don't want me to write the code for you. 您说“任何指针”-我认为那意味着您希望我为您编写代码。

Update 更新

This is closely based on your original code, so that it would be as easy as possible for you to understand. 这与您的原始代码紧密相关,因此您将尽可能地容易理解。 I have changed the bare minimum that I needed to change, to get this working. 我已经更改了我需要更改的最低要求,以使它正常工作。

int counter = 1;
long prod = 1;   
long total = 0;

n = console.nextInt(); 

while (counter <= n) 
{                  
    prod = prod * counter;
    if( counter % 4 == 1 ) {
        total += prod;
    } else if (counter % 4 == 3) {
        total -= prod;
    }

    counter++; 
}   

System.out.println(total);

First up, notice that I have changed prod to a long . 首先,请注意我将prod更改为long That's because factorials get very big very fast. 那是因为阶乘变得非常快。 It would be even better to use a BigInteger , but I'm guessing you haven't learnt about these yet. 使用BigInteger甚至会更好,但是我想您还没有了解这些。

Now, there are those two conditions in there, for when to add prod to the total, and when to subtract prod from the total. 现在,有两个条件,何时将prod添加到总数中,以及何时从总数中减去prod These both work by checking the remainder when counter is divided by 4 - in other words, checking which factorial we're up to, and doing the right operation accordingly. 两者都通过检查counter除以4时的余数来进行工作-换句话说,检查我们要处理的阶乘,并相应地执行正确的操作。

First of all, you need to introduce a variable int sum = 0; 首先,您需要引入一个变量int sum = 0; to store the value of the alternate series. 存储备用序列的值。

To only sum every second value, you should skip every second value. 要仅对第二个值求和,应跳过第二个值。 You can check that using the modulo operation, eg if( counter % 2 == 1 ) . 您可以使用模运算来检查它,例如if( counter % 2 == 1 )

If that is true, you can add/subtract the current value of prod to the sum. 如果是这样,则可以将prod的当前值加/减到总和。

To get the alternating part, you can use a boolean positive = true; 要获得交替部分,可以使用boolean positive = true; like this: 像这样:

if( positive ) {
    sum += prod;
} else {
    sum -= prod;
}
positive = !positive;

Based on the boolean , the prod is either added or subtracted. 基于boolean ,可以对prod进行添加或减去。 The value is altered afterwards. 该值随后更改。

Because factorials become very large very fast, it would be better to use variables of type long . 因为阶乘会变得非常大,所以最好使用long类型的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM