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
vote
1 answer
While loop doesn't break when it should
I was just writing a little python program to run the Collatz algorithm and I think the while loop isn't breaking when it should. I'm running this whith VSCode 1.44.2 and Python 3.7.5 64bit
def collatz(n):
while n != 1.0:
if n%2 == 0:
…

sevi
- 460
- 5
- 20
1
vote
1 answer
Collatz from automate the boring stuff
I know there are multiple posts on this question. But I could not post my code in any other way except by asking a question. Can someone please help me understand how I can stop n from being input into the collatz function each time the global scope…

Khajan Joshi
- 11
- 1
1
vote
1 answer
Determining a program's execution time by its length in bits?
This is a question popped into my mind while reading the halting problem, collatz conjecture and Kolmogorov complexity. I have tried to search for something similar but I was unable to find a particular topic maybe because it is not of great value…
user12546101
1
vote
1 answer
How to properly use if else statements and while loops with a child process in C
I'm new to C and I've been trying to create a program that takes a user input integer makes a sequence depending on whether the number is even or odd.
n / 2 if n is even
3 * n + 1 if n is odd
A new number will be computed until the sequence…

Chigozie A.
- 335
- 4
- 16
1
vote
2 answers
main function does not call collatzSequencer function
I am currently working on a project for college, first year computing class. With that being said, I am not asking for the answer, but I am asking more for some advice. In order to begin the project, I have decided to create a function called…

Jonathan Jerai
- 55
- 3
1
vote
5 answers
Python 3: Optimizing Project Euler Problem #14
I'm trying to solve the Hackerrank Project Euler Problem #14 (Longest Collatz sequence) using Python 3. Following is my implementation.
cache_limit = 5000001
lookup = [0] * cache_limit
lookup[1] = 1
def collatz(num):
if num == 1:
…

Bilesh Ganguly
- 3,792
- 3
- 36
- 58
1
vote
1 answer
More than 2 threads working slower than 1 or 2 threads unless Thread.sleep(1) is put in the run() method of a thread
The task I'm trying to implement is finding Collatz sequence for numbers in a set interval using several threads and seeing how much improvement is gained compared to one thread.
However one thread is always faster no matter if it I choose 2…

Svajunas Kavaliauskas
- 191
- 2
- 5
- 15
1
vote
1 answer
The sequence I am calculating (hailstone) is printing twice when it needs to print once
So in the main of my program there is a section where I call on the hailstone function to answer a question in a print statement.
Instead of it printing the sequence once, it is printing it twice and I don't know why or how to fix it.
Is there a…
1
vote
3 answers
Collatz conjecture: Count how many have the length 111
So my question is about the collatz conjecture. The task is that I have to write a code which counts the length of steps of each conjecture. For example 2 = 2/2 = 1 is one step.
Now I need to know how many start numbers between 1 and 10000 have 111+…

Mohannad Webman
- 19
- 1
- 3
1
vote
3 answers
recursive function python 3 'maximum recursion depth exceeded'
I am creating a collatz sequence with a recursive function below:
def collatz(n):
if n%2 == 0:
return int(n/2)
else:
return int((3 * n)/2)
From what I understand, a recursive function is a function that basically calls itself. Below…

Harris2018
- 503
- 1
- 5
- 13
1
vote
1 answer
collatz sequence infinte loop error
I am getting an infinite loop. I am not sure on how to covert the result as the new number variable and put it back in the while loop.
#Collatz squence
import sys
def collatz():
try:
print('Enter a number')
…

Kartik Hegde
- 13
- 4
1
vote
1 answer
Finding long Collatz sequences in Haskell
Today, I am playing with Collatz sequences in Haskell ...
Here is my code:
collatzLength :: Int -> Integer -> Int
collatzLength count 1 = count
collatzLength count n = collatzLength (count+1) $ if n `mod` 2 == 0 then n `div` 2 else 3*n+1
main =
…

ols
- 173
- 7
1
vote
1 answer
Haskell : Problem converting result of division to integral type
I'm learning Haskell and stuck trying to understand the type system.
I'm trying to write a function which returns the length of the series 'Half or Three Plus One' for an input. Here's my attempt at the function, using a recursive approach (the…

vicky.dev
- 115
- 1
- 6
1
vote
4 answers
How can I stop my Collatz Sequence code at 1?
I tried implementing the Collatz Sequence using a while loop, but I can't stop the sequence at 1. The code continues. I tried using all my possibilities, but I can't come up with a solution.
import java.util.Scanner;
public class cs{
public…

Ramana
- 49
- 4
1
vote
1 answer
Collatz Conjecture palindrome
I'm supposed to write code that shows the collatz conjecture in 3 different ways for an assignment using recursion. If you're not familiar with the idea, the conjecture states that if you take any starting value n you can ultimate get to the value…

Caladin00
- 356
- 1
- 3
- 11