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
3
votes
1 answer
how to write Collatz sequence using unfold in scheme/racket?
After writing the Collatz sequence generating function the regular way:
(define (colatz-seq #;starting@ n)
(cond ((= n 1) '())
((even? n) (cons (/ n 2) (colatz-seq (/ n 2))))
((odd? n) (cons (+ (* n 3) 1) (colatz-seq (+ (* n 3)…

X10D
- 600
- 2
- 13
3
votes
1 answer
Optimize Collatz Conjecture in Java
I'm working on a program that determines the number of steps it takes for a number to become 1 using the Collatz Conjecture (if n is odd, 3n+1; if n is even, n/2). The program increases the number being calculated by one each time it completes a…

NotGene
- 41
- 4
3
votes
2 answers
Print the collatz sequence of a positive int, n, one value per line stoping at 1 using python
I've done:
def collatz(n):
seq = n
if n == 1:
n = n
while n > 1:
if n % 2 == 0:
n = n // 2
else:
n = 3 * n + 1
print(seq)
The corrct output for calling this function, while n = 10:…

Frank
- 339
- 8
- 18
3
votes
3 answers
Error "No instance for (Num [t])" in Collatz function
I am new to Haskell, and programming in general. I am trying to define a function which generates the sequence of Collatz numbers from n. I have:
collatz n = (collatz' n) : 1
where collatz' n = (takeWhile (>1) (collatz'' n))
where…

emi
- 5,380
- 1
- 27
- 45
3
votes
2 answers
How can I write a Collatz sequence with List::Gen?
I love the functional-programming paradigm that List::Gen brings to Perl. Writing a Collatz sequence with it should be doable, albeit a little challenging since the length of the list is not known a priori.
I'm missing the final 1 at the end of the…

Zaid
- 36,680
- 16
- 86
- 155
3
votes
3 answers
Project Euler 14 Java
I have been having trouble on Problem 14 on Project Euler. I don't understand why my code(Java) isn't working and any help would be appreciated.
public class Calculate {
public static void main(String[] args){
…

monsterbananas
- 41
- 5
3
votes
8 answers
Why doesn't this loop terminate?
Here's the sample code:
public static void col (int n)
{
if (n % 2 == 0)
n = n/2 ;
if (n % 2 != 0)
n = ((n*3)+1) ;
System.out.println (n) ;
if (n != 1)
col (n) ;
}
this works just fine until it gets down…

David
- 14,569
- 34
- 78
- 107
3
votes
3 answers
trying to predict future results, collatz, Perl
I am working on a collatz sequence. I currently have a for loop.
for my $num (1..1000000) {
my $count = 1;
for (my $i = $num; $i != 1; $count++) {
$i = $i % 2 ? 3 * $i + 1 : $i / 2;
}
}
And then I have a simple way of working…

Runt
- 55
- 5
3
votes
3 answers
translate list comprehension into Common Lisp loop
I have very recently started learning lisp. Like many others, I am trying my hand at Project Euler problems, however I am a bit stuck at Problem 14 : Longest Collatz Sequence.
This is what I have so far:
(defun collatz (x)
(if (evenp x)
(/…

martin
- 676
- 4
- 9
3
votes
4 answers
Too long runtime of euler project #14 in Java
My code of Euler Project 14 is below. I have run this code for more than 3 hours with output result and it seems to be infinite. When I test a single number such as 11, 27, it will output the collatz chain number:14 and 111 quickly. But I don't know…

mitcc
- 343
- 4
- 10
3
votes
1 answer
Scheme while loop
I'm kinda new in scheme syntax... I'm trying to make a simple program where you input an integer, if the integer is even do something and if it's odd do something else.
I was able to do this part. Now, I need to make a loop where I can decrement the…

Muhsag
- 155
- 3
- 11
3
votes
3 answers
Java: Finding the largest chain in a Collatz sequence
I cannot find what's wrong for Project Euler problem #14. My first step was finding the algorithm, which worked until the numbers hit around 120000. The code broke and realized that I needed to use BigIntegers. I converted my algorithm to meet that…

Brian
- 167
- 1
- 9
3
votes
2 answers
collatz-list implementation using Prolog
I am trying to create a function called collatz_list in Prolog. This function takes two arguments, the first one is a number and the second in a list. This list will be my output of this function. So, here's my function:…

Stranger
- 864
- 4
- 21
- 48
3
votes
4 answers
Longest Collatz Sequence
While doing my Java homework which is to implement the Collatz Conjecture, I thought of a different objective which is to find the longest Collatz sequence. My program counts the steps as follows:
public class Collatz {
static int count = 0;
…

Alpan Karaca
- 968
- 5
- 12
- 30
3
votes
8 answers
Uva's 3n+1 problem
I'm solving Uva's 3n+1 problem and I don't get why the judge is rejecting my answer. The time limit hasn't been exceeded and the all test cases I've tried have run correctly so far.
import java.io.*;
public class NewClass{
/**
*…

andandandand
- 21,946
- 60
- 170
- 271