0 Replies - 367 Views - Last Post: 02 September 2010 - 08:18 AM

#1 captainhampton   User is offline

  • Jawsome++;
  • member icon

Reputation: 15
  • View blog
  • Posts: 548
  • Joined: 17-October 07

Collatz Conjecture

Posted 02 September 2010 - 08:18 AM

Description: Enter any natural number n into the function to see the algorithm perform and reach its destination of 1.Collatz conjecture assumes any natural number conforming to the algorithm will eventually yield 1.
# The Collatz Conjecture is an unsolved problem in mathematics. The general
# form of the algorithm is to take any natural number n. If n is
# even divide it by 2 to get n/2, if n is odd multiply it by 3 and 
# add 1 to obtain 3n+1. Repeat until you reach 1. 

def Collatz(n):
	count = 0
	while n != 1:
		if n == 1:
			count += 1
			break
		if n % 2 == 0:
			if n == 1:
				count += 1	
				break
			n = n/2
			count += 1
		if n % 2 != 0:
			if n == 1:
				count += 1	
				break
			n = 3*n+1
			count += 1	
	return count


Is This A Good Question/Topic? 0
  • +

Page 1 of 1