An algorithm given by Lothar Collatz produces sequences of integers, and is described as follows:
It has been shown that this algorithm will always stop (in step 2) for initial values ofAas large as 109, but some values ofAencountered in the sequence may exceed the size of an integer on many computers. In this problem we want to determine the length of the sequence that includes all values produced until either the algorithm stops (in step 2), or a value larger than some specified limit would be produced (in step 4).
Input
The input for this problem consists of multiple test cases. For each case, the input contains a single line with two positive integers, the first giving the initial value ofA(for step 1) and the second givingL, the limiting value for terms in the sequence. Neither of these,AorL, is larger than 2,147,483,647 (the largest value that can be stored in a 32-bit signed integer). The initial value ofAis always less thanL. A line that contains two negative integers follows the last case.
Output
For each input case display the case number (sequentially numbered starting with 1), a colon, the initial value forA, the limiting valueL, and the number of terms computed.
Sample Input
3 100 34 100 75 250 27 2147483647 101 304 101 303 -1 -1
Sample Output
Case 1: A = 3, limit = 100, number of terms = 8 Case 2: A = 34, limit = 100, number of terms = 14 Case 3: A = 75, limit = 250, number of terms = 3 Case 4: A = 27, limit = 2147483647, number of terms = 112 Case 5: A = 101, limit = 304, number of terms = 26 Case 6: A = 101, limit = 303, number of terms = 1
#define RUN #ifdef RUN #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include <string> #include <iostream> #include <sstream> #include <map> #include <set> #include <vector> #include <list> #include <cctype> #include <algorithm> #include <utility> #include <math.h> using namespace std; #define MAXN 105 long long A, limit; int cnt; int casenum = 1; void play(int casenum){ cnt = 1; long long aA = A; while(true){ if(aA == 1){ printf("Case %d: A = %lld, limit = %lld, number of terms = %d\n", casenum, A, limit, cnt); return; } if(aA%2 == 0){ aA = aA/2; } else{ aA = 3*aA + 1; } if(aA > limit){ printf("Case %d: A = %lld, limit = %lld, number of terms = %d\n", casenum, A, limit, cnt); return; } ++cnt; } } int main(){ #ifndef ONLINE_JUDGE freopen("694.in", "r", stdin); freopen("694.out", "w", stdout); #endif while(scanf("%lld%lld", &A, &limit)==2 && A!=-1 && limit!=-1){ play(casenum); ++casenum; } } #endif
相关推荐
stack exec -- collatz slave localhost 7001 & stack exec -- collatz slave localhost 7002 & stack exec -- collatz master localhost 7000 或使用提供的launch.sh脚本。 主机将在终端上等待,直到您按下RETURN...
欧拉·科拉兹(Euler Collatz)项目Collatz问题为正整数的集合定义了以下迭代序列: n→n / 2(n为偶数) n→3n + 1(n为奇数) ...,该方法将n到1的Collatz序列返回编写方法longest_collatz ,该方法返回
在数组中编写一个方法collatz(n) ,该方法将n到1的Collatz序列返回 编写方法longest_collatz ,该方法返回小于一百万的起始数字,该方法返回最长的序列 运行learn直到所有RSpec测试通过。 来源 -- 在Learn.co上...
在数组中编写一个方法collatz(n) ,该方法将n到1的Collatz序列返回 编写方法longest_collatz ,该方法返回小于一百万的起始数字,该方法返回最长的序列 运行learn直到所有RSpec测试通过。 来源 --
在数组中编写一个方法collatz(n) ,该方法将n到1的Collatz序列返回 编写方法longest_collatz ,该方法返回小于一百万的起始数字,该方法返回最长的序列 运行learn直到所有RSpec测试通过。 来源 --
1.collatz序列 编写一个名为 collatz()的函数,它 有一个名为 number 的参数。如果参数是偶数, 那么 collatz()就打印出 number // 2,并返回该值。如果 number 是奇数,collatz()就打 印并返回 3 * number + 1。 ...
An Automated Approach to the Collatz Conjecture
考拉兹猜想 无人能解的最简单数学问题的直观表示。 (3N+1 问题) 又名3N+1问题。 臭名昭著的 Collatz 猜想说,如果您从任何正整数开始,您将始终以这个循环结束。 结果,在某些时候,最终会出现在4,2,1的循环...
cs371p-collatz
collatz_visualization-源码.rar
cs371p-collatz cs371p 项目 1
cs371p-collatz CS371p 的项目#1
collatz.pdf
定义了Collatz波集CWS及作用在其上的3-1映射triim,探讨了CWS在triim映射下的性质,从另一个角度对Collatz猜想进行了探讨,以期为最终证明Collatz猜想提供了一个全新的思路。
主要介绍了Python Collatz序列实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
matlab开发-collatzmaps网络。两个函数显示了从著名的collatz序列映射中获得的复杂动态。
Collatz工具是一个用于查找大量collatz问题和已修改的collatz问题的应用程序。 您可以修改collatz因子,即最大数量。 计算,输出。 使用C ++和bigInt库完成以获取任意精度的整数。
三个不同的 M 文件,用于检查 Collatz 序列。
主要介绍了Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture,本文直接给出实现代码,代码中包含详细注释,需要的朋友可以参考下
此函数检查给定数字的 Collatz 猜想。 可以自定义检查与 3n+1 不同的其他变体取一个数 n。 如果是偶数,则除以 2。如果是奇数,则乘以 3 并加 1。重复。 所有起始数字都会导致 1 吗?