×

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 = ( Sx S2 x S3 ) + 1 = ( 3 x 4 x 13 ) + 1 = 157

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

S6 = ( Sx 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

Recursion Program in Java

The recursion program in Java demonstrates the usage of recursion. The process by which a function/ method calls itself, again and again, is called recursion. Each recursive call is pushed...

10 minutes read.

Java For Loop

A for loop is used to execute a set of statements for a fixed number of times. It takes the following form: for (initialization; condition; update) { statements; } The for loop defines...

2 minutes read.

Java Serialization

JAVA SERIALIZATION Serialization is a process by which objects can be represented as a sequence of bytes. These bytes have information about object's data, object's type and datatypes of members in...

3 minutes read.

ArrayList VS Linked List

ArrayList VS Linked List Both the ArrayList and LinkedList implements the List interface, and they have some differences as well as some similarities between them. The internal working and performance of both vary significantly. Let’s...

7 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

Java Obfuscator

Obfuscation is the process of making something unclear or difficult to interpret. Obfuscators are being used in programming to protect the source code from hackers. In this article, we will...

8 minutes read.

Best Java Libraries

One of the most widely used programming languages is Java. Java has a large number of libraries, including the standard Java library that includes libraries such as java.lang, java.util, and...

7 minutes read.

Sleep Time in Java

Sleep is amethod in java that is related to thread class and it is a concept of multithreading and is used to stop the execution of the current thread a...

4 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

Compile time vs Runtime in java

Introduction: This article will discuss compile time vs. runtime in java. Compile time and runtime are two programming terms utilized in software improvement. Compile time is when the source code is...

3 minutes read.

Java Boolean toString() Method

The toString() method of Java Boolean class returns a String corresponding to this Boolean object. It returns a string value “true”, if the defined object is true else it returns...

2 minutes read.

Java File Input Stream

Java makes use of stream ideas to speed up input and output processes. All packages of input and output streams are contained in java.io.package. Stream: A stream is nothing more...

5 minutes read.

Java Integer reverseByte() method

The reverseByte() method of Java Integer class returns the value obtained by reversing the order of the bytes in the 2’s complement binary representation. Syntax public static int reverseByte (int i) Parameters The parameter...

1 minute read.

Bouncy Number in Java

We will define bouncy numbers in this section and write Java programmes to determine whether a specific number is bouncy. Java coding exams and academic assignments usually inquire about the...

3 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.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.