×

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

Circular Linked List

Circular Linked List A circular linked list where all nodes are connected to their next node and last node is connected to the starting node or we can say all nodes...

5 minutes read.

Find Number of Minimum Insertion to Make a String Palindrome

You have been given a string. You have to find out the number of minimum insertions to make this string palindrome. The string will contain only lower case alphabets. Note:What is...

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

Binary Search

Binary Search: When there is a large data structure, the linear search takes a lot of time to search the element. The binary search was developed to overcome the lack...

7 minutes read.

Dynamic memory allocation of structure in C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

5 minutes read.

Stack vs Queue: Data Structure

 Difference Between Stack and Queue What is Stack? The LIFO principle applies on insertion and deletion operations of the stack which means last inserted element to the stack will remove first....

3 minutes read.

Insertion Sort vs Bubble Sort

In this article, we will see the major differences between Insertion Sort and Bubble Sort. Before that, let’s have a quick overview of what these sorting algorithms are and what’s...

4 minutes read.

Bubble sort algorithm using Javascript

Sorting is a very useful technique in many algorithms and programs. Basically, sorting operations help us to arrange a set of data in a particular manner. Bubble sort is one...

3 minutes read.

What Is Dfs Algorithm in Data Structures

DFS stands for Depth First Search. Generally, it is a repetitive or decidable type of algorithm which is basically used in identifying all the vertices or nodes of a graph...

5 minutes read.

Queue operations in Data Structure

Queue - Queue is a linear data structure or first in first out data structure means the first element added in the queue will be removed first and the last...

7 minutes read.

Heap Sort vs Merge Sort

In this article, we are going to discuss the Heap Sort, Merge sort and the difference between them. What is Heap Sort? Heap – A heap is an abstract data type categorised...

7 minutes read.

Right side view of binary tree

The right view of the binary tree is generally known to be that side viewed from the right direction of the point of view. To be more precise, the right-side...

8 minutes read.

Traversal of binary tree

Traversal of binary tree: A node is visited only once in the traversal of the binary tree. There are three main types of traversal methods in the binary tree. In-order traversalPre-order...

3 minutes read.

Compare Balanced Binary Tree and Complete Binary Tree

Complete and balanced binary trees are important and general topics in the concept – Tree data structure. Before discussing the complete and balanced binary tree, we need to have an...

8 minutes read.

Spanning Tree

Spanning Tree: The spanning tree is a subset of the graph. It is a non-cyclic graph. If any node in the spanning tree is truncated, the entire graph fails. There are...

10 minutes read.

Treap data structure

In this article, we will discuss the treap data structure. The word treap is a combination of 'tree' and 'heap'. So, treap data structure is a combination of a heap...

8 minutes read.

Assembly Line Scheduling

If we take an example of a car factory, there are two assembly lines. In an assembly line, we can assemble and repair the parts of a car. Now, suppose...

5 minutes read.

Optimal binary search tree in DSA

Implementation // A simple way of the recursive implementation of the optimal search that we will perform on the binary tree.   #include <bits/stdc++.h> using namespace std; // we have to create a basic utility...

8 minutes read.

String Operations in Data Structures

Operations on Strings Reversing the order of words in a sentence Reversing a string is a technique that reverses or alters the order of a given string so that the last character...

9 minutes read.

Detect and Remove Loop in a Linked List

Create a function called detectAndRemovetheLoop() that verifies whether a given Linked List has a loop, eliminates the loop if it does, and returns true if it does. It returns false...

6 minutes read.