如何在Java中檢查一個數是否為三角數?


自然數‘n’的三角數可以定義為從1到n的所有自然數之和。

它被稱為三角數,因為我們可以使用總點數(行數等於點數)在等邊三角形網格中表示它。意思是第1行1個點,第2行2個點,第3行3個點,依此類推。

簡單來說,如果一個數是從1開始的所有連續數字之和,那麼它就是一個三角數。

Mathematically n*(n+1)/2

在這篇文章中,我們將學習如何使用Java程式語言來檢查一個數是否為三角數。

舉幾個例子:

示例1

輸入數字是10

讓我們使用三角數的邏輯來檢查它:

1 + 2 + 3 + 4 = 10 which is the sum of all consecutive numbers starting from 1.

因此,10是一個三角數。

示例2

輸入數字是15

讓我們使用三角數的邏輯來檢查它:

1 + 2 + 3 + 4 + 5 = 15 which is the sum of all consecutive numbers starting from 1.

因此,15是一個三角數。

示例3

輸入數字是7

讓我們使用三角數的邏輯來檢查它:

1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10

所以,從上面我們知道7不是一個三角數。

其他一些三角數的例子包括1、3、6、21、55等等。

演算法

  • 步驟1 - 獲取一個整數,可以透過初始化或使用者輸入獲得。

  • 步驟2 - 使用迴圈迭代從1到輸入數字。不斷累加每個數字,同時檢查在任何時候和是否等於原始輸入數字。

  • 步驟3 - 如果在任何時候和等於原始輸入數字,則中斷迴圈並列印它是三角數;否則列印它不是三角數。

多種方法

我們提供了三種不同的方法來解決這個問題。

  • 使用靜態輸入值

  • 使用使用者自定義方法

  • 查詢一個數的三角數

讓我們逐一檢視程式及其輸出。

方法1:使用靜態輸入值

在這種方法中,將在程式中初始化一個整數值,然後使用演算法檢查該數是否為三角數。

示例

public class Main{ public static void main(String args[]){ //Declared an integer variable and initialized a number as value int originalNumber = 6; //printing the given number to be checked System.out.println("Given number: "+originalNumber); //declaring an integer variable sum to hold the sum value //initializing it with value as 0 int sum = 0; //declare boolean variable activeFlag as false boolean activeFlag = false; // Loop that adds consecutive digits for(int i = 1; i<=originalNumber; i++){ sum = sum + i; //if sum is equal with original number //then make the activeFlag as true; if(sum == originalNumber){ activeFlag = true; break; } } //if activeFlag is true then given number is triangular number //print it is triangular number if(activeFlag == true) System.out.println(originalNumber+" is a triangular number"); //else print given number is not a triangular number else { System.out.println(originalNumber+" is not a triangular number"); } } }

輸出

Given number: 6
6 is a triangular number

方法2:使用使用者自定義方法

在這種方法中,將在程式中初始化一個整數值,然後我們將呼叫一個使用者自定義方法,並將此給定數字作為引數傳遞。

在方法內部,我們將使用演算法檢查該數是否為三角數。

示例

public class Main{ public static void main(String args[]){ //Declared an integer variable and initialized a number as value int originalNumber = 55; //printing the given number to be checked System.out.println("Given number: "+originalNumber); //call the user defined method to check a number is triangular or not //by passing the input number as parameter if(checkTriangular(originalNumber)){ System.out.println(originalNumber+" is a triangular number"); } else { System.out.println(originalNumber+" is not a triangular number"); } } //user defined method to check triangular number static boolean checkTriangular(int originalNumber){ //declaring an integer variable sum to hold the sum value //initializing it with value as 0 int sum = 0; // Loop that adds consecutive digits for(int i = 1; i<=originalNumber; i++){ sum = sum + i; if(sum == originalNumber){ return true; } } return false; } }

輸出

Given number: 55
55 is a triangular number

方法3:查詢一個數的三角數

我們將找到給定數字的三角數。意思是給定一個數字,比如‘n’,我們必須找到n*(n+1)/2。

示例

public class Main{ //main method public static void main(String[] args){ //declared and initialized an integer variable int inputNumber = 7; //declared an integer variable and initialize with value 0 int triangularNumber = 0; //using loop we will find the sum of consecutive numbers from 1 to that number for(int i = 1 ; i<=inputNumber ; i++){ triangularNumber = triangularNumber + i; } System.out.println(triangularNumber+" is the triangular number for "+inputNumber); } }

輸出

28 is the triangular number for 7

在這篇文章中,我們探討了如何使用不同的方法在Java中檢查一個數是否為三角數。

更新於:2022年10月28日

3K+ 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.