The Collatz Conjecture is a conjecture that a certain algorithm always terminates. The algorithm is stated as follows: starting with some positive integer n, divide n by two if it is even and otherwise triple n and add one. The algorithm terminates when n reaches one. It is currently an open problem whether this terminates for all positive integers. It is also called the Hailstone Sequence.
Questions tagged [collatz]
284 questions
-1
votes
1 answer
Hailstone Sequence in Java with ArrayList
Hello I am attempting to perform the hailstone sequence.
A hailstone sequence is basically: take a given integer n - if even, the next integer in the sequence is n/2, if odd, the next integer in sequence is n * 3 + 1.
The API I must follow for my…

Ben Schmidt
- 5
- 3
-1
votes
1 answer
Collatz sequence getting last number 1 repeated
// Define the recursive function.
int collatz(int p1)
{
// While Loop Starting
while (p1>1)
{
//when the number is even
if(p1%2==0)
{
p1 = p1/2;
printf("%d ", p1);
…

chandu
- 27
- 4
-1
votes
2 answers
Hailstone sequence using nested while loops
I'm writing a program that allows the user to input a range of numbers, and then the program will preform a hailstone sequence of each number in the range and will then print the number with the largest cycle length. I cant see why my code isn't…

Rsherrill
- 129
- 3
- 4
- 12
-1
votes
2 answers
3n+1 on UVa (C++)
The link to the 3n+1 problem on UVa is:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36
My code is :
#include
using namespace std;
long long l(long long n)
{
if(n==1)
{
…

Rahul Chhabra
- 11
- 7
-2
votes
1 answer
Return biggest number after writing out coalltz sequence of given number
So if I write out a collatz sequence of the given number long int n in my first function then in the next I wanna return the biggest number how do I do take for example if I call the first writeCollatzSekvens(7) it writes out
7 22 11 34 17 52 26 13…

Snozye
- 27
- 4
-2
votes
1 answer
Why does this code not work with the integer 113383?
It's to find out how many operations are needed until the integer is calculated to 1 (Collatz problem).
It works with every integer except 113383. With 113383 the console just doesn't do anyhting, it doesn't print ops, it seems as the program is…

VincentSchaerl
- 97
- 4
-2
votes
1 answer
Code to simulate Collatz Conjecture is showing 1 as an even number
My code is the following
output_even = "Even: "
output_odd = "Odd: "
if varnum % 2 == 0:
varnum /= 2
print(output_even, varnum)
time.sleep(0.1)
elif varnum % 2 != 0:
varnum *= 3
varnum += 1
print(output_odd, varnum)
time.sleep(0.1)
Output…

Quantum
- 1
- 4
-2
votes
1 answer
Python collecting outputs into a list?
Instead of printing the numbers in the sequence it should collect them in a list and return the list.
Here is the code that prints numbers in the sequence:
def collatz_sequence(n):
while n > 1:
print(n)
if n % 2 == 1:
…

Mason
- 7
-2
votes
3 answers
What is the problem with my code Longest Collatz Sequence?
I have been trying to solve this problem :
The following iterative sequence is defined for the set of positive
integers:
n → n/2 (n is even) n → 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following
sequence: 13…

محمد المغربى
- 13
- 2
-2
votes
1 answer
Collatz conjeceture for each number that is between the two given numbers
num = Integer.parseInt(tf1.getText());
entered = Integer.parseInt(tf1.getText());
num2 = Integer.parseInt(tf2.getText());
entered2 = Integer.parseInt(tf2.getText());
for (i =(int) num; i<= num2 ; i++){
for (j=0 ; j >= i ; j++) {}
…

Noor Eddin Hasan
- 90
- 1
- 7
-2
votes
1 answer
Collatz Sequence: Efficiency problem or software/hardware too slow?
I think have worked out a solution for finding the largest Collatz Sequence below a LIMIT but it takes a lot of time! My question really is where the source for this would be: code, software, hardware?
I have done some research on the Internet and…

Petersson
- 1
- 1
-2
votes
1 answer
How to turn the numbers in the sequence generated by the collatz function into a string by using concatenation
The numbers in the sequence generated by the Collatz function will be put in a
string. Building this string is likely the hardest part of this. Get the
computation working without worrying about creating the string. Now, I'm sure I'm getting…

Danlin
- 1
-2
votes
3 answers
return not exiting recursive method as expected - C#
This should be fairly basic.
I feel like i'm missing something glaringly obvious but I've been staring at this for a while now.
I've got a recursive method creating a Collatz sequence (using as recursion practice)
However once my method reaches the…

Thomas Fox
- 521
- 2
- 5
- 16
-2
votes
1 answer
Longest Collatz Sequence - Prokect Euler #14
The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)
n → 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
It…

sanorpetro
- 413
- 1
- 4
- 10
-2
votes
3 answers
Why is this variably turning negative?
So my error says that I don't have the rights to access this memory.
This is the code for it, I'm trying to get the collaz series working.
But my n is getting negative even though it shoudln't.
const int number = 1000000;
//Chain…

slow
- 566
- 4
- 17