Primality Test | Set 5 ( Using Lucas | Java program to find out Lucas - Lehmer series . ; Function to find out first n terms ( considering 4 as 0 th term ) of Lucas - Lehmer series . ; the 0 th term of the series is 4. ; create an array to store the terms . ; compute each term and add it to the array . ; print out the terms one by one . ; Driver Code
import java . util . * ; class GFG { static void LucasLehmer ( int n ) { long current_val = 4 ; ArrayList < Long > series = new ArrayList < > ( ) ; series . add ( current_val ) ; for ( int i = 0 ; i < n ; i ++ ) { current_val = current_val * current_val - 2 ; series . add ( current_val ) ; } for ( int i = 0 ; i <= n ; i ++ ) { System . out . println ( " Term ▁ " + i + " : ▁ " + series . get ( i ) ) ; } } public static void main ( String [ ] args ) { int n = 5 ; LucasLehmer ( n ) ; } }