Given a number n, find out number of Symmetric Relations on a set of first n natural numbers {1, 2, ..n}.

Examples:

Input : n = 2 Output : 8 Given set is {1, 2}. Below are all symmetric relation. {} {(1, 1)}, {(2, 2)}, {(1, 1), (2, 2)}, {(1, 2), (2, 1)} {(1, 1), (1, 2), (2, 1)}, {(2, 2), (1, 2), (2, 1)}, {(1, 1), (1, 2), (2, 1), (1, 2)} Input : n = 3 Output : 64

A Relation ‘R’ on Set A is said be Symmetric if xRy then yRx for every x, y ∈ A

or if (x, y) ∈ R, then (y, x) ∈ R for every x, y?A

Total number of symmetric relations is

2.^{n(n+1)/2}

How does this formula work?

A relation R is symmetric if the value of every cell (i, j) is same as that cell (j, i). The diagonals can have any value.

There are n diagonal values, total possible combination of diagonal values = 2

^{n}

There are n^{2}– n non-diagonal values. We can only choose different value for half of them, because when we choose a value for cell (i, j), cell (j, i) gets same value.

So combination of non-diagonal values = 2^{(n2 – n)/2}Overall combination = 2

^{n}* 2^{(n2 – n)/2}=2^{n(n+1)/2}

## C++

`// C++ program to count total symmetric relations ` `// on a set of natural numbers. ` `#include <bits/stdc++.h> ` ` ` `// function find the square of n ` `unsigned ` `int` `countSymmetric(unsigned ` `int` `n) ` `{ ` ` ` `// Base case ` ` ` `if` `(n == 0) ` ` ` `return` `1; ` ` ` ` ` `// Return 2^(n(n + 1)/2) ` ` ` `return` `1 << ((n * (n + 1))/2); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `unsigned ` `int` `n = 3; ` ` ` ` ` `printf` `(` `"%u"` `, countSymmetric(n)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count total symmetric ` `// relations on a set of natural numbers. ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `// function find the square of n ` ` ` `static` `int` `countSymmetric(` `int` `n) ` ` ` `{ ` ` ` `// Base case ` ` ` `if` `(n == ` `0` `) ` ` ` `return` `1` `; ` ` ` ` ` `// Return 2^(n(n + 1)/2) ` ` ` `return` `1` `<< ((n * (n + ` `1` `)) / ` `2` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` `System.out.println(countSymmetric(n)); ` ` ` `} ` `} ` ` ` ` ` `// This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to count ` `# total symmetric relations ` `# on a set of natural numbers. ` ` ` `# function find the square of n ` `def` `countSymmetric(n) : ` ` ` `# Base case ` ` ` `if` `(n ` `=` `=` `0` `) : ` ` ` `return` `1` ` ` ` ` `# Return 2^(n(n + 1)/2) ` ` ` `return` `(` `1` `<< ((n ` `*` `(n ` `+` `1` `))` `/` `/` `2` `)) ` ` ` ` ` `# Driver code ` ` ` `n ` `=` `3` `print` `(countSymmetric(n)) ` ` ` `# This code is contributed ` `# by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count total symmetric ` `// relations on a set of natural numbers. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function find the square of n ` ` ` `static` `int` `countSymmetric(` `int` `n) ` ` ` `{ ` ` ` `// Base case ` ` ` `if` `(n == 0) ` ` ` `return` `1; ` ` ` ` ` `// Return 2^(n(n + 1)/2) ` ` ` `return` `1 << ((n * (n + 1)) / 2); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 3; ` ` ` `Console.WriteLine(countSymmetric(n)); ` ` ` `} ` `} ` ` ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count total symmetric ` `// relations on a set of natural numbers. ` ` ` `// function find the square of n ` `function` `countSymmetric(` `$n` `) ` `{ ` ` ` `// Base case ` ` ` `if` `(` `$n` `== 0) ` ` ` `return` `1; ` ` ` ` ` `// Return 2^(n(n + 1)/2) ` ` ` `return` `1 << ((` `$n` `* (` `$n` `+ 1))/2); ` `} ` ` ` ` ` `// Driver code ` ` ` `$n` `= 3; ` ` ` `echo` `(countSymmetric(` `$n` `)); ` ` ` `// This code is contributed by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

Output:

64

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- A square matrix as sum of symmetric and skew-symmetric matrices
- Number of Reflexive Relations on a Set
- Minimum flip required to make Binary Matrix symmetric
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's
- Prime Number of Set Bits in Binary Representation | Set 2
- Prime Number of Set Bits in Binary Representation | Set 1
- Count number of subsets of a set with GCD equal to a given number
- Count number of triplets with product equal to given number with duplicates allowed | Set-2
- Calculate the number of set bits for every number from 0 to N
- Smallest number dividing minimum number of elements in the array | Set 2
- Minimum number of squares whose sum equals to given number N | set 2
- Find the largest number smaller than integer N with maximum number of set bits
- Minimum number of squares whose sum equals to a given number N | Set-3
- Number of factors of very large number N modulo M where M is any prime number
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Count total set bits in all numbers from 1 to n | Set 2
- Find next greater number with same set of digits
- Bell Numbers (Number of ways to Partition a Set)
- Program to convert a given number to words | Set 2
- Given a number n, count all multiples of 3 and/or 5 in set {1, 2, 3, ... n}

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.