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整数,并且函数通过将该整数作为参数进行调用。相关输出将显示在控制台上。