ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมที่จะใช้ Colatz 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