Home > Backend Development > C++ > Written in C++, find the number of reflexive relations on a set

Written in C++, find the number of reflexive relations on a set

PHPz
Release: 2023-08-26 20:17:22
forward
960 people have browsed it

In this article, we will explain ways to find reflexive relationships on a set. In this problem, we are given a number n, and a set of n natural numbers, and we must determine the number of reflexive relations.

Reflexive relation - If for every 'a' in set A, (a, a) belongs to relation R, then relation R is said to be a reflexive relation on set A. For example -

Input : x = 1
Output : 1
Explanation : set = { 1 }, reflexive relations on A * A :
{ { 1 } }

Input : x = 2
Output : 4
Explanation : set = { 1,2 }, reflexive relations on A * A :
   { ( 1, 1 ) , ( 2, 2 ) }
   { ( 1, 1 ), ( 2, 2 ), ( 1, 2 ) }
   { ( 1, 1 ), ( 2, 2 ), ( 1, 2 ), ( 2, 1 ) }
   { ( 1, 1 ), ( 2, 2 ), ( 2, 1 ) }
Copy after login

Thus, if for every element a ∈ A, there is (a, a) ∈ R, then the relation R is reflexive.

Solution method

The number of reflexive relations on the element set can be calculated through the formula 2n2−n. This general formula is obtained by counting the number of reflexive relations of integers.

Written in C++, find the number of reflexive relations on a set

Example

#include <iostream>
using namespace std;
int countReflexive(int n){
    int ans = 1 << (n*n - n);
    return ans;
}
int main(){
    int n ;
     cin >> n ; // taking input n from the user using std cin.
    int result = countReflexive(n); // calling function to calculate number of reflexive relations
    cout << "Number of reflexive relations on set: " << result ; // printing the answer
    return 0;
}
Copy after login

Output

Number of reflexive relations on set: 1
Copy after login

Explanation of the above program

This program is easy to understand because we just Get the input from the user and put it into the formula 2n2−n. We use the left shift operator "

Conclusion

In this paper, we addressed a problem about the number of reflexive relations on sets. We discussed simple ways to solve a given problem, and mathematicians derived a formula for counting the number of reflexive relations.

We also learned to write a program for this problem in C, with a time complexity of O(1). We can write the same program in other languages ​​like C, Java, Python and others.

The above is the detailed content of Written in C++, find the number of reflexive relations on a set. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template