求和的 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.