इस ट्यूटोरियल में, हम Collatz Conjecture को लागू करने के लिए एक प्रोग्राम पर चर्चा करेंगे।
इसके लिए हमें एक संख्या n दी जाएगी और हमें यह पता लगाना होगा कि क्या इसे दो संक्रियाओं का उपयोग करके 1 में बदला जा सकता है -
-
यदि n सम है, तो n को n/2 में बदल दिया जाता है।
-
अगर n विषम है, n को 3*n + 1 में बदल दिया जाता है।
उदाहरण
#include<bits/stdc++.h>
using namespace std;
//checking if n reaches to 1 or not
bool check1(int n, unordered_set<int> &s){
if (n == 1)
return true;
if (s.find(n) != s.end())
return false;
return (n % 2)? check1(3*n + 1, s) :
check1(n/2, s);
}
bool if_one(int n){
unordered_set<int> s;
return check1(n, s);
}
int main(){
int n = 234;
if_one(n) ? cout << "Yes" : cout <<"No";
return 0;
} आउटपुट
Yes