×

Recaman’s Sequence

Recamán's succession repeat connection in arithmetic and software engineering. Since its components are obviously connected with the past components, they are as often as possible characterized utilizing recursion.

It takes its name after its pioneer Bernardo Recaman Santos (Bogota, August 5, 1954), a Colombian mathematician.

Recaman's course of action was named after its trailblazer, Colombian mathematician Bernardo Recaman Santos, by Neil Sloane, creator of the On-Line Encyclopedia of Integer Sequences (OEIS).

The OEIS section for this grouping is A005132.

In any event, when Neil Sloane has gathered in excess of 325,000 arrangements starting around 1964, the Recamán's succession was referred to in his paper My #1 number sequences.

He moreover communicated that of the huge number of groupings in the OEIS, this one is his #1 to listen to[1] (you can hear it underneath).

Definition:

The Recaman's succession a0, a1, a2 is characterized as:

Recaman’s Sequence

Recaman's Sequence

The initial terms of the grouping are:

0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9, 24, 8, 25, 43, 62, 42, 63, 41, 18, 42, 17, 43, 16, 44, 15, 45, 14, 46, 79, 113, 78, 114, 77, 39, 78, 38, 79, 37, 80, 36, 81, 35, 82, 34, 83, 33, 84, 32, 85, 31, 86, 30, 87, 29, 88, 28, 89, 27, 90, 26, 91, 157, 224, 156, 225, 155, ...

//printing first n elements

Input: n=7
Output: 0,1,3,6,2,7,13
Input: n=4
Output: 0,1,3,6

It is basically a capability with domain and co-domain as regular numbers and 0 separately. It is characterized recursively as follows:

Make a(n) to signify the (n+1)- th term. (0 is as of now present).

As indicated by Rules:

a(0) = 0,
on the off chance that n > 0 and the number isn't
   currently remembered for the grouping,
     a(n) = a(n - 1) - n
else
     a(n) = a(n-1) + n.

A straightforward execution is displayed beneath, in which we store all n Recaman Sequence numbers in an exhibit. Utilizing the recursive recipe referenced above, we process the following number.

  • Time Complexity : O(n)
  • Space Complexity : O(n)

Advantages:

Recamán's grouping, not with standing its numerical and tasteful properties, can be utilized to scramble 2D pictures utilizing steganography.

// Java programm to print nth number in Recaman's arrangement
import java.io.*;


class  Rsequence {
           // Prints first n terms of Recaman arrangement
	static void recaman(int n)
	{
		// Make an exhibit to store terms
		int arr[] = new int[n];
                      // Initial term of the succession is generally 0
		arr[0] = 0;
		System.out.print(arr[0]+" ,");


		// Fill remaining terms utilizing recursive
		// equation.
		for (int j = 1; j< n; j++)
		{
			int curr = arr[j - 1] - i;
			int k;
			for (k = 0; k < i; k++)
			{
				// In the event that arr[j-1] - i is negative or
				// as of now exists.
				if ((arr[k] == curr) || curr < 0)
				{
					curr = arr[j - 1] + i;
					break;
				}
			}


			arr[j] = curr;
			System.out.print(arr[j]+", ");


		}
	}


	// Driver code
	public static void principal (String[] args)
	{
		int n = 19;
		recaman(n);


	}
}

Visual Presentation:

Recaman’s Sequence

Using Array:

Given a set of n positive whole numbers addressing lengths.

Decide the most extreme conceivable region whose four sides are browsed the given exhibit.

 It ought to be noticed that a square shape must be framed assuming the given exhibit contains two sets of equivalent qualities.

Method1(Sorting):

The errand fundamentally decreases to finding two sets of equivalent qualities in cluster.

On the off chance that there are multiple matches, pick the two sets with greatest qualities.

A straightforward arrangement is to do following.

Sort the given cluster.

Navigate exhibit from biggest to littlest worth and return two sets with most extreme qualities

// CPP program for tracking down most extreme region conceivable
// of a square shape
#incorporate <bits/stdc++.h>
utilizing namespace sexually transmitted disease;


// capability for tracking down max region
int findArea(int array[], int n1)
{
    // sort cluster in non-expanding request
    sort(arr, array + n1, greater<int>());


    // Introduce different sides of square shape
    int dimension[2] = { 0, 0 };


    // navigate through exhibit
    for (int i = 0, j = 0; i < n1 - 1 && j < 2; i++)


        // assuming any component happens two times
        // store that as aspect
        if (array[i] == array[i + 1])
            dimension[j++] = arr[i++];


    // return the result of aspects
    return (dimension[0] * dimension[1]);
}


// driver capability
int primary()
{
    int arr[] = { 4, 2, 1, 4, 6, 6, 2, 5 };
    int n1 = sizeof(array)/sizeof(array[0]);
    cout << findArea(array, n1);
    bring 0 back;
}

Method2(Hashing):

In a hash set, all first events of components are embedded.

Monitor something like two qualities for second events

// CPP program for tracking down greatest region conceivable
// of a square shape
#incorporate <bits/stdc++.h>
utilizing namespace sexually transmitted disease;


// capability for tracking down max region
int findArea(int arr[], int n)
{
    unordered_set<int> s1;


    // cross through exhibit
    int firstt= 0, secondd = 0;
    for (int i = 0; i < n; i++) {


        // In the event that this is first event of arr[i],
        // basically supplement and proceed
        if (s1.find(array[i]) == s1.end()) {
            s1.insert(array[i]);
            proceed;
        }


        // Assuming that this is second (or more) event,
        // update first and second greatest qualities.
        if (array[i] > firstt) {
            secondd = firstt;
            firstt = array[i];
        } else if (array[i] > secondd)
            secondd = array[i];
    }


    // return the result of aspects
    return (firstt * secondd);
}


// driver capability
int fundamental()
{
    int arr[] = { 4, 2, 1, 4, 6, 6, 2, 5 };
    int n1 = sizeof(array)/sizeof(array[0]);
    cout << findArea(array, n1);
    bring 0 back;
}
# Python 3 program to print n-th
# numbering in Recaman's grouping


# Prints first n terms of Recaman
# group
def recaman(n1):


    # Make a cluster to store terms
    array = [0] * n1


    # Initial term of the succession
    # is always generally 0
    array[0] = 0
    print(array[0], end=", ")


    # Fill remaining terms utilizing
    # recursive 
    for i in range(1, n1):


        current = array[i-1] - i
        for j in range(0, i):


            # If array[i-1] - I is
            # negative or as of now
            # exists.
            if ((array[j] == current) or current < 0):
                current = arr[i-1] + I
                break


        array[i] = current
        print(array[i], end=", ")


# Driver code
n1 = 15


recaman(n1)

Related Topics

Bubble Sort vs Selection Sort

In this article, we will discuss the basic differences between these two sorting algorithms. Let us have a quick overview of what these sorting algorithms are? And what are the...

6 minutes read.

Berkley’s Algorithm

Berkley’s Algorithm is mainly used in clock synchronization system. It is used in distributed systems. To implement this algorithm, we have to think that the network has no accurate time...

4 minutes read.

A Full Binary Tree with n Nodes

Implementation // Writing the implementation of the above approach in C++ #include <bits/stdc++.h> using namespace std; // We are creating a class that will create a node and its left and right children.  struct __nod...

12 minutes read.

Given Two Binary Trees, Check if it is Symmetric

Implementation // creating a C++ program that will help us check whether the two given trees are mirror images of each other.  #include<bits/stdc++.h> using namespace std; /* A given binary tree has a data...

5 minutes read.

Linear Queue VS Circular Queue

What is Queue? A queue is one of the important linear data structures extensively used in various computer applications. It is based on the FIFO (First In First Out) principle. It...

9 minutes read.

Applications of Different Linked Lists in Data Structure

What is a Linked list? A linked list is a data structure that consists of a sequence of elements, where each containing a reference or ("link") to the next element in...

5 minutes read.

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.

What are Forest Trees in Data Structure

Data structure A data model manages and optimizes computer resources, and a database stores and manages data. It's one of many uses for data structures to hold data. Data structures come...

5 minutes read.

Does Overloading Work with Inheritance

This is a question that occasionally comes to many programmers. Who are curious to know more now has a complete explanation and a solution through this tutorial! Inheritance: The functions of...

3 minutes read.

Balanced Binary Tree

A balanced binary tree is just a random nod-based tree with a rule of keeping its height minimum in size to maintain various operations such as insertions, deletions and several...

3 minutes read.

Convert Binary Tree into a Threaded Binary Tree

Implementation /*Writing a C++ program that will help us change the binary tree into a threaded binary tree and help us transform. */ #include <bits/stdc++.h> using namespace std; /*Creating the structure of a node...

11 minutes read.

Flatten Binary Tree to a linked list

Implementation In this section, we will see the implementation of the binary Tree and its conversion into linked lists. let us proceed: - // Writing a C++ program that will convert a...

4 minutes read.

Deletion Operation of the binary search tree in C++ language

A typical binary search tree implements some order to carry out the arrangements. As the name suggests, each parent node should have at most two children. The main rule in...

4 minutes read.

FLEX (Fast Lexical Analyzer Generator)

FLEX stands for Fast Lexical Analyzer Generator. Around 1987, Vern Paxson created Flex in C with a great deal of input and inspiration from Van Jacobson. Van Jacobson's approach is...

3 minutes read.

Print kth least significant bit number

You have given a number and you have to find out the kth least significant bit of this number. K will be given to you.  The bit will be from...

3 minutes read.

Extended Binary Tree

An extended binary tree is a binary tree in which all the NILL subtrees present mainly in the original trees are exchanged with the special nodes that are primarily known...

3 minutes read.

Winner tree in Data Structures

Tree Data structure A tree is a hierarchical and non-linear data structure with nodes. Each node in the Tree contains a message value and stores the name passed to another ("child")...

6 minutes read.

Deletion Operation from A B Tree

This article will show the deletion operation through the b tree in C++ programming language. Implementation #include <iostream> using namespace std; class B_TreeNod {   int *kys;   int m;   BTreeNod **C;   int j;   bool leaf;  ...

5 minutes read.

Cocktail Sort

C Program executes cocktail sort. Combo sort is a somewhat straightforward arranging calculation initially planned by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered by Stephen Lacey and Richard Box...

5 minutes read.

B+ Tree in Data Structure

A B-Tree extension called B+ Tree, which enables effective search, insertion, and deletion operations. Both Records and keys can be stored in internal and leaf nodes in a B tree. Contrarily,...

4 minutes read.