Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,184 members, 7,807,609 topics. Date: Wednesday, 24 April 2024 at 04:07 PM

Please Help With This - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Please Help With This (703 Views)

(2) (3) (4)

(1) (Reply) (Go Down)

Please Help With This by SPLENDID25(m): 11:37am On Aug 17, 2020
Write a function named collatz() that has one parameter named number. If number is even, then collatz() should print number // 2 and return this value. If number is odd, then collatz() should print and return 3 * number + 1. Then write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1. (Amazingly enough, this sequence actually works for any integer—sooner or later, using this sequence, you’ll arrive at 1! Even mathematicians aren’t sure why. Your program is exploring what’s called the Collatz sequence, sometimes called “the simplest impossible math problem.”) Remember to convert the return value from input() to an integer with the int() function; otherwise, it will be a string value. Hint: An integer number is even if number % 2 == 0, and it’s odd if number % 2 == 1.
Re: Please Help With This by stanliwise(m): 6:21pm On Aug 17, 2020
@SPLENDID25
What have you coded so far?. Are you experiencing some issues or what?
Re: Please Help With This by Najdorf: 8:31pm On Aug 17, 2020
It hasn't been proved that it works for all positive integers.

What's the problem though? The problem looks straightforward.
Re: Please Help With This by SPLENDID25(m): 6:28pm On Aug 18, 2020
stanliwise:
@SPLENDID25
What have you coded so far?. Are you experiencing some issues or what?
I am actually a beginner. its actually a practice problem. I was able to write the function, but unable to write the part that should call the function and ask for an input provided the returned value isn't 1. I need hints from seniors.
Re: Please Help With This by SPLENDID25(m): 6:31pm On Aug 18, 2020
Najdorf:
It hasn't been proved that it works for all positive integers.

What's the problem though? The problem looks straightforward.
I need hints on how to go about the problem. I have been thinking, for the past three days now.
Re: Please Help With This by stanliwise(m): 8:37pm On Aug 18, 2020
SPLENDID25:
I am actually a beginner. its actually a practice problem. I was able to write the function, but unable to write the part that should call the function and ask for an input provided the returned value isn't 1. I need hints from seniors.
what language are you using??
Re: Please Help With This by saheedniyi22(m): 9:44pm On Aug 18, 2020
SPLENDID25:
Write a function named collatz() that has one parameter named number. If number is even, then collatz() should print number // 2 and return this value. If number is odd, then collatz() should print and return 3 * number + 1. Then write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1. (Amazingly enough, this sequence actually works for any integer—sooner or later, using this sequence, you’ll arrive at 1! Even mathematicians aren’t sure why. Your program is exploring what’s called the Collatz sequence, sometimes called “the simplest impossible math problem.”) Remember to convert the return value from input() to an integer with the int() function; otherwise, it will be a string value. Hint: An integer number is even if number % 2 == 0, and it’s odd if number % 2 == 1.

I remember this from automate the boring stuff with Python, I couldn't solve it then let me try it now.
Re: Please Help With This by Taofeekdboy(m): 9:51pm On Aug 18, 2020
SPLENDID25:
I need hints on how to go about the problem. I have been thinking, for the past three days now.
I will give you a hint, the best to use for this practice is recursive function. It calls itself. Please read more on it.
Re: Please Help With This by codeigniter(m): 12:16am On Aug 19, 2020
def collatz ():
if number % 2 == 0:
return number // 2
elif number % 2 == 1:
return 3 * number + 1

number = int(input())

while number != 1:
number = collatz ()
print(number)

sorry for d indentation
I have answer dis question b4,

before u ask question use the search bar to see if such question has not been asked before

join www.africonn.com
I hope this helps
Re: Please Help With This by SPLENDID25(m): 4:07pm On Aug 19, 2020
stanliwise:
what language are you using??
Python
Re: Please Help With This by SPLENDID25(m): 4:18pm On Aug 19, 2020
Taofeekdboy:

I will give you a hint, the best to use for this practice is recursive function. It calls itself. Please read more on it.
okay I will. thanks
Re: Please Help With This by SPLENDID25(m): 4:21pm On Aug 19, 2020
codeigniter:
def collatz ():
if number % 2 == 0:
return number // 2
elif number % 2 == 1:
return 3 * number + 1

number = int(input())

while number != 1:
number = collatz ()
print(number)

sorry for d indentation
I have answer dis question b4,

before u ask question use the search bar to see if such question has not been asked before

join www.africonn.com
I hope this helps
Sir, every input returned 1 and the program ends.
Re: Please Help With This by stanliwise(m): 4:24pm On Aug 19, 2020
SPLENDID25:
Python
use the input() functionality.
To get integer you can use:
int(input(“input a number”))

Just store it a variable and you can attach it to a while loop to keep calling it.
Re: Please Help With This by SPLENDID25(m): 4:38pm On Aug 19, 2020
stanliwise:

use the input() functionality.
To get integer you can use:
int(input(“input a number”))

Just store it a variable and you can attach it to a while loop to keep calling it.
def collatz (number):
if number % 2 == 0:
return number // 2
else:
return 3 * number + 1
number = int(input())

I used the while loop, followed by invoking the function. all it did was to continue printing the returned value.
Re: Please Help With This by stanliwise(m): 4:46pm On Aug 19, 2020
SPLENDID25:
def collatz (number):
if number % 2 == 0:
return number // 2
else:
return 3 * number + 1
number = int(input())

I used the while loop, followed by invoking the function. all it did was to continue printing the returned value.
When is your while loop suppose to terminate?
Re: Please Help With This by codeigniter(m): 4:48pm On Aug 19, 2020
SPLENDID25:
Sir, every input returned 1 and the program ends.

is that not the problem u want to solve
Re: Please Help With This by SPLENDID25(m): 4:49pm On Aug 19, 2020
stanliwise:

When is your while loop suppose to terminate?
when the returned value equals 1.
Re: Please Help With This by stanliwise(m): 4:52pm On Aug 19, 2020
SPLENDID25:
when the returned value equals 1.
if it fails to return 1 then it means your loop is infinite.
Try to input value that could make your function return 1
Re: Please Help With This by SPLENDID25(m): 4:54pm On Aug 19, 2020
codeigniter:


is that not the problem u want to solve
I mean, using your code. Every input returned 1 and the program terminates. its not supposed to be that way.
Re: Please Help With This by SPLENDID25(m): 5:21pm On Aug 19, 2020
stanliwise:
if it fails to return 1 then it means your loop is infinite.
Try to input value that could make your function return 1
def collatz (number):
if number % 2 == 0:
return number // 2
else:
return 3 * number + 1
number = int(input())
collatz (number)
print (collatz (number))
while collatz (number) != 1:
print (collatz (number))

the program above did with 2 as input, but if the returned value isn't 1, it prints the returned value infinitely.
Re: Please Help With This by Najdorf: 5:55pm On Aug 19, 2020
stanliwise:
if it fails to return 1 then it means your loop is infinite.
Try to input value that could make your function return 1
Going farther than this coding exercise, this is a popular unsolved math problem. There is no known way to check if a number will reach 1. But there is also no known counterexample of a number that does not eventually reach 1.
Re: Please Help With This by SPLENDID25(m): 9:42am On Aug 20, 2020
codeigniter:
def collatz ():
if number % 2 == 0:
return number // 2
elif number % 2 == 1:
return 3 * number + 1

number = int(input())

while number != 1:
number = collatz ()
print(number)

sorry for d indentation
I have answer dis question b4,

before u ask question use the search bar to see if such question has not been asked before

join www.africonn.com
I hope this helps
Taofeekdboy:

I will give you a hint, the best to use for this practice is recursive function. It calls itself. Please read more on it.
saheedniyi22:


I remember this from automate the boring stuff with Python, I couldn't solve it then let me try it now.
Najdorf:
It hasn't been proved that it works for all positive integers.

What's the problem though? The problem looks straightforward.
stanliwise:
@SPLENDID25
What have you coded so far?. Are you experiencing some issues or what?
I successfully written the program. Thanks to everyone for his or her contributions.

1 Like

(1) (Reply)

Help Needed On C / Autel IM608 Add Iveco Daily 2021 Key Via OBD / Are You Taking Advantage Of Chatgpt As A Programmer?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 29
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.