×

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and 3. We shall calculate the other terms of the Sylvester sequence using the first two terms.

Example

S1 = 3 , and S2 = 4Apple increases the prices of its US-based subscriptions!

S3 = ( S1 x S2 ) + 1 = ( 3 x 4 ) + 1 = 13

S4 = ( S1 x S2 x S3 ) + 1 = ( 3 x 4 x 13 ) + 1 = 157

S5 = ( S1  x S2 x S3 x S4 ) + 1 = ( 3 x 4 x 13 x 157 ) + 1 = 24493

S6 = ( S1 x S2 x S3 x S4 x S5 ) + 1 = (3 x 4 x 13 x 157 x 24493) + 1 = 599882557

Explanation

In the above example first and second terms are 3 and 4. The third term will product of first and second i.e., 3 and 4 and plus 1. So the third term is ( 3 * 4 ) + 1 =13. Fourth term will be product of first 3 terms plus 1 i.e., ( 3 * 4 * 13 ) +1 = 157.Likethis the Sylvester sequence goes on.

Using Loops

The goal is to create a loop that multiplies both variables by the arithmetic modular operation (a + b) % N = ( a % N + b % N ) % N ) , where N is a modular number, in order to store the product up to this point and the current number, which is just the first number plus 1.

SylvesterSequence1. java

//  import statement to import required packages 
import java . util . ArrayList ;  
 // public class with name SylvesterSequence1 is defined
public class SylvesterSequence1  
{  
// main method of the program where execution of the program starts  
public static void main ( String args [ ] )   
{  
//  list for storing the Sylvester sequence  
ArrayList <Integer> a1 = new ArrayList < Integer > ( ) ;  
// Adding the first two terms of the sequence  
a1.add(3);  
a1.add(4);  
  // for computing the first five terms  
int n = 5; 
System . out . println ( " The first " + n + " terms of the Sylvester's Sequence : " ) ;  
//  using loops to traverse through the iterations
for ( int j = 0; j < n; j++ )  
{  
int r = 1 ;  
// if j=0 and j=1 it should print first term or second term
if ( ( j == 0) || ( j == 1) )  
{  
// It prints the  first two terms of the sequence  
System . out . print ( a1 . get ( j ) + " " ) ;  
// continue statement is used to skip the present iteration of the loop
continue;  
}  
// using another for loop to calculate the nth term of the sequence by multiplying with previous term in the arraylist
for(int k = 0; k < a1 . size ( ) ; k++ )  
{  
 r = r * a1 . get (k ) ;  
}  
  // adding 1 to the result
r =  r + 1;  
// adding it to the list
a1 . add ( r ) ;  
// printing the sequence  
System . out . print ( r + " " ) ;  
} // for loop for calculating nth term
}  // main
}  //  SylvesterSequence1

Output:

Sylvester Sequence in Java

Analysis of Complexity

 Because the programme uses nested loops, its temporal complexity is O (n2). The programme employs an array list as well, increasing its O-level space complexity (n).

Optimal Approach

// Using the  import statment to import the required packages 
import java . util . ArrayList ;   
public class Main   
{  
  
public void Sequence(int x)  
{  
int a = 1;  // for storing the product.  
int r = 2 ; // for storing the n th number.  
for (int i = 1; i <= x; i++)   
{  
System.out.print( r + " ");  
r = a * r;  
a = r ;  
r = ( r + 1 ) ;  
}  
}  
// main method of the program where execution of the program starts  
public static void main ( String args [ ] )    
{  
// creating an object for  the class SylvesterSeq2  
Main o1 = new Main ( ) ;
Scanner sc = new Scanner ( System . in ) ;
System . out . println ( " Enter the number of terms required in the sequence ");
// for computing the first six terms  
int n=sc . nextInt ( ) ;
System . out . println ( " The first " + n + " terms of the Sylvester's Sequence are: " ) ;  
o1 . Sequence ( n ) ;  
}  
}  

Output

Sylvester Sequence in Java

Complexity Analysis

The program's space complexity is O ( 1  ) , whereas its time complexity is O(n).


Related Topics

Object Oriented Programming (OOPs)

Since the dawn of earth, we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the...

5 minutes read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers...

2 minutes read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.

Aggregation in Java

Aggregation A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation....

4 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

5 minutes read.

How to Convert String to Date in Java

How to Convert String to Date in java You can convert String to Date in Java by using the parse() method. There are two classes which have parse() method namely, DateFormat and SimpleDateFormat classes....

2 minutes read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

Crown Pattern in Java

We know the importance of solving pattern problems. We can solve pattern problems by using any programming language. There is no rule to translating into a particular programming language. We...

4 minutes read.

The Diamond Operator in Java 7

The Diamond operator is a comparatively recent Java operator originally developed in JDK 7 to enhance type identification and eliminate boilerplate Java code. The Diamond operator is characterized by a...

2 minutes read.

How to uninstall the Java in Ubuntu

We need java on our computers in our daily lives because there are lots of different applications that are developed using the java environment, so we have to install java...

4 minutes read.

String Array in Java

String Array in Java An array is alinear data structure that stores similar type of data. It allows us to store fixed number of elements.It can be of different data types...

6 minutes read.

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

How to Call a Method in Java

In Java, a method is a collection of statements that perform a specific task or action.It can accept data with the help ofitsarguments. It  is also called a function. In order...

9 minutes read.

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.

Thread Synchronization in Java

In Java, the smallest processing component is a thread, which is a small subprocess. It follows a different course of action. Threads are autonomous. If an exception occurs in one thread,...

6 minutes read.

Java Math pow() Method

The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab. Syntax: public static double pow(double a, double b) Parameters: The parameters ‘a’ and...

3 minutes read.

Find Unique Elements in Array Java

An array in Java is a grouping of objects that share the same type of data. We are free to enter identical or repeating elements into an array. Therefore, we...

6 minutes read.