求和的 Java 程序,其中第 n 项为 n^2 – (n-1)^2


以下是计算此类级数之和的 Java 程序 -

示例

 在线演示

public class Demo {
   static long my_val = 1000000007;
   public static long compute_val(long my_int){
      return ((my_int % my_val) * (my_int % my_val)) % my_val;
   }
   public static void main(String[] args){
      long my_int = 45687234;
      System.out.println("The computed value is ");
      System.out.print(compute_val(my_int));
   }
}

输出

The computed value is
335959495

名为 Demo 的一个类定义了一个名为“compute_val”的函数,该函数计算并返回特定级数的和。在 main 函数中定义一个 long 整数,并通过将该整数作为参数传递调用该函数。相关的输出显示在控制台。

更新于: 2020 年 7 月 13 日

193 次浏览

启动你的 职业

完成课程即可获得认证

开始
广告
© . All rights reserved.