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
2 answers
collatz conjecture to print the number of objects and the sequence using class
#include
using namespace std;
class ulam
{
int num;
double prod;
int cot;
public:
ulam(){cot=0;}
ulam(int x)
{
num=x;
}
void process()
…

ishfaq miraj
- 21
- 1
2
votes
3 answers
Collatz function not exiting correctly
Here's a program that is intended to count the length of a Collatz sequence recursively:
def odd_collatz ( n ):
return (3 * n) + 1
def even_collatz ( n ):
return int(n / 2)
def collatz_counter ( initialNumber, initialLength ):
length =…

superstewie
- 71
- 1
- 6
2
votes
2 answers
Longest Collatz (or Hailstone) sequence optimization - Python 2.7
I've made a program that prints out a list of numbers, each one with a greater number of steps (according to the Collatz Conjecture) needed to reach 1 than the previous:
limit = 1000000000
maximum = 0
known = {}
for num in xrange(2, limit):
…

SorenLantz
- 177
- 2
- 14
2
votes
2 answers
What is wrong with my "Collatz conjecture"?
So, I was just trying to create a loop to run the "3n+1" formula and when I enter a negative number I get stuck in an infinite loop with a remainder of 0 and -1.
Is that correct or is my code missing something?
Here is my code:
Scanner scan =…

Timinycricket
- 149
- 9
2
votes
1 answer
Push numbers into an Array, then push that Array into another Array - JavaScript
I am creating a program which takes two variables as user prompts. The first one is a starting number, the next is a number to determine a range. The program then spits out how many steps it takes for the original number entered to reach zero using…

Kyle Bing
- 253
- 2
- 6
- 20
2
votes
5 answers
Why does this blow up the heap in Lispworks?
I'm trying to solve Problem 14 in Project Euler (find the longest Collatz sequence between 1 and 1000000).
My code consists of a recursive, memoized function to calculate the length of Collatz sequences followed by a loop to find the maximum. Please…

Paulo Mendes
- 697
- 5
- 16
2
votes
3 answers
How to keep track of recursive call on my function collatz?
I am trying to keep track of how many times the function call itself. I have tried setting up num as 0 and putting num = num+1 at the end but I keep getting 1. How do I fix this?
function [out num] = collatz(val)
num = 0;
if val == 1
out =…

Jason Thapa
- 123
- 9
2
votes
1 answer
Multiple Constructors in Prolog
I was trying to implement various forms of queries on Hailstone Sequence.
Hailstone sequences are sequences of positive integers with the following properties:
1 is considered the terminating value for a sequence.
For any even positive integer i,…

sarvagya
- 25
- 1
- 4
2
votes
1 answer
how to optimize this Haskell program?
I use the following code to memoize the total stopping time of Collatz function by using a state monad to cache input-result pairs.
Additionally the snd part of the state is used to keep track of the input value that maximizes the output, and the…

Javran
- 3,394
- 2
- 23
- 40
2
votes
2 answers
Maple Sequence Length
I'm trying to create a basic program in Maple that runs the Collatz sequence when given a number (n) from the user. For those that don't know, the Collatz sequence is basically "If the number given is odd, do 3n + 1, if it is even, divide by 2 and…

Rhys Terry
- 31
- 1
- 8
2
votes
1 answer
Haskell beginner: "No instance for...arising from..." error
My goal is to write a function that calculates the maximum Collatz number below a certain number 'n'. (It's a Project Euler question for those who are familiar.)
Some context: A Collatz number for a given integer is equal to the length of the…

iceman
- 2,020
- 2
- 17
- 24
2
votes
1 answer
Longrunning python script to create collatz graph
I've been trying to write a python script to make a graph of the iterations it takes for a number to go through the collatz conjecture. In this example I only used a very small range (Only the number 1) but this script seems to keep running and will…

steam.preacher
- 23
- 4
2
votes
2 answers
collatz c code logical error
static void collatz(int i)
{
int x=0,a=0,res=0,count=0;
int array[50];
array[0]=i;
while(array[count]!=0)
{
if(array[count]%2==0)
{
count++;
array[count]=i/2;
}
else
…

user3278628
- 21
- 1
2
votes
1 answer
Project Euler 14(Collatz Conjecture), issue implementing cache in Java
I was solving Project Euler problems and I'm stuck at number 14, I did manage to solve it using brute force, the problem states:
http://projecteuler.net/problem=14
The following iterative sequence is defined for the set of positive integers:
n →…

skynil
- 23
- 5
2
votes
3 answers
Collatz function in scheme
So i'm trying to solve the collatz function iteratively in scheme but my test cases keep showing up as
(define (collatz n)
(define (collatz-iter n counter)
(if (<= n 1)
1
(if (even? n) (collatz-iter…

user2789945
- 527
- 2
- 6
- 23