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
-2
votes
1 answer
UVA 3n+1 (prob 100) what is Wrong in my C++ program
I tried solving the 3n+1 problem(UVa 100),here is my code but according to UVa online judge my program gives a wrong answer,my code passed all the test cases i can think of but unable to detect what is wrong, please help me find the…

J Ajay
- 3
- 1
-2
votes
2 answers
3n+1 solution giving wrong answer
This is my solution to 3n+1 problem which is giving wrong answer. I have struggled on this one quiet a lot many times since the last 5 days. Please help me figure out the problem in my solution. I have used tail recursion and am also storing a map…

tranquil
- 499
- 7
- 13
-2
votes
2 answers
I am trying to run a Collatz conjecture sequence to see if it always ends at 1, but my number is stuck at 999. Why is this?
Here is my code:
def isEven(number):
return number % 2 == 0
def run(x):
z = x
while z != 1:
if isEven(z) == True:
z = z/2
print z
else:
z = (3*z)+1
print z
else:
…
user3268208
-3
votes
1 answer
Maximum Collatz length in Python, but output is not correct
This is my first time posting to Stackoverflow.
I'm trying to solve this problem here: https://codingbat.com/prob/p270692?parent=/home/konstans@stuy.edu/all
When looking at all hailstone sequences from 1 to z, maxHail(z) will return the starting…

Emperor Concerto
- 97
- 3
-3
votes
2 answers
Automate The Boring Stuff - Collatz Sequence
I'm new to Python and I having issues with my Collatz Sequence project in the Automate the Boring Stuff book. When I run my code it "prints" 2 of each number. I can't figure out why it is duplicating each number. Anyone have any ideas?
Here are the…

smillerlou
- 1
- 1
-3
votes
1 answer
I don't get a print result of function when using range
Why can't I see the result of the function when I use range()? I want to create a range of numbers where each the number will be evaluate in "colla" function. But range doesn't work with "colla"
def colla(num):
fin = []
while num != 1:
…

Gen Pol
- 9
- 4
-3
votes
2 answers
How does one make a python turtle to draw outputs of the collatz sequence in a line graph?
def next_term(num):
if num<=0:
print("Zero or negative numbers are not even, nor Odd.","Enter number >",num)
else:
print(int(num))
while num!=1: #number is even
if num%2==0:
…
-3
votes
1 answer
Automatically generate list from math function?
My idea is to run the 3n + 1 process (Collatz conjecture) on numbers ending in 1, 3, 7, and 9, within any arbitrary range, and to tell the code to send the lengths of each action to a list, so I can run functions on that list separately.
What I have…

Russ wB
- 3
- 2
-3
votes
3 answers
Basic Algorithm Optimization
My current assignment is to create a basic algorithm that finds the magic number (which is 1). The magic number can be found by either dividing an even number by 2 until it's magic, or multiplying an odd number by 3, adding 1, the dividing by 2…

Aeternal
- 1
- 4
-3
votes
1 answer
3n+1 uVa gives WA
My initial question was a bit vague, so here's the edited question.
The 3n+1 uVa problem is based on the Collatz Conjecture.
Consider any positive integer 'n'. If it is even, divide it by 2. If it is odd, multiply it by 3 and add 1. After 'x' such…

Saunved Mutalik
- 381
- 2
- 19
-4
votes
3 answers
how to solve 3x+1 on python
from random import randrange
a = randrange(1, 9)
print (a)
if (a % 2) == 0:
y = a / 2
while True:
if (y % 2) == 0:
y = y / 2
print (y)
else:
…

Esbah
- 9
- 1
- 5
-5
votes
2 answers
why < is much faster than !=?
Problem : Consider the following algorithm to generate a sequence of
numbers. Start with an integer n. If n is even, divide by 2. If n is
odd, multiply by 3 and add 1. Repeat this process with the new value
of n, terminating when n = 1. The…

amfad33
- 74
- 8
-6
votes
3 answers
14 - Project Euler - Java - Incorrect Answer - Bug?
I cannot find the bug in this code. If it were not for Project Euler ruling my answer as incorrect, I would swear to the heavens that my code is right.
I could use another approach, but this problem is not all that complex, and yet I have been…

BenParker93
- 51
- 1
- 1
- 3
-7
votes
3 answers
Do you know how to write this Scheme function?
Could you write a function that takes one argument (a positive integer) and
divides it by two if it's even, or
multiplies it by three and adds one if it's odd
and then returns the resulting number.
And then a separate function that takes one…

dave
- 135
- 1
- 10