Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,166,071 members, 7,863,783 topics. Date: Tuesday, 18 June 2024 at 06:14 AM |
Nairaland Forum / Science/Technology / Programming / Please Help With This (734 Views)
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: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: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:what language are you using?? |
Re: Please Help With This by saheedniyi22(m): 9:44pm On Aug 18, 2020 |
SPLENDID25: 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 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:Python |
Re: Please Help With This by SPLENDID25(m): 4:18pm On Aug 19, 2020 |
Taofeekdboy:okay I will. thanks |
Re: Please Help With This by SPLENDID25(m): 4:21pm On Aug 19, 2020 |
codeigniter:Sir, every input returned 1 and the program ends. |
Re: Please Help With This by stanliwise(m): 4:24pm On Aug 19, 2020 |
SPLENDID25: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: def collatz (number): 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:When is your while loop suppose to terminate?def collatz (number): |
Re: Please Help With This by codeigniter(m): 4:48pm On Aug 19, 2020 |
SPLENDID25: 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 the returned value equals 1. |
Re: Please Help With This by stanliwise(m): 4:52pm On Aug 19, 2020 |
SPLENDID25: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: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: def 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: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: Taofeekdboy: saheedniyi22: Najdorf: stanliwise:I successfully written the program. Thanks to everyone for his or her contributions. 1 Like |
(1) (Reply)
Should I Build A Forum From Scratch Or Use Wordpress / I Need A UI Designer Budget 50k / I Urgently Need A Wordpress Developer
(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. 37 |