# 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

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:

## 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)
```