实现科拉茨猜想
在本教程中,我们将讨论一个实现科拉茨猜想的程序。
为此,我们将获得一个数字 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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP