3x+1 in python, correct?
how do u keep intendation when posted? its intendented now as i see it but it comes out unintendented.
main question: is this a correct implementation of the 3x+1-program listed some posts below? http://www.linuxquestions.org/questi...roblem-631581/ Code:
a=raw_input("do the 3x+1 on this number: ") also in the else-part do i need to do a=a*3.0+1 or is it ok with just 3? |
[ code]
put code here [/code] no space between the [] and "code". Also if you have javascript enabled you can just hit the "#" button when you post and it'll do it for you. |
Code:
a=raw_input("what's the number, master? ") (Wow, it's been a while since the last time I wrote anything in Python. I forgot how beautiful it is..) |
Use the // operator instead of / if you want to do int division.
|
Quote:
|
Nope.
Originally, Python's / operator did floor division if its operands were both of type int or long, and true division otherwise. That is, 1/2 == 0, but 1.0/2.0 == 0.5. That behavior works fine for a statically-typed language like C, but in Python it caused problems. For example, consider the function Code:
def mean(num_list): The // operator does floor division, even with floats. Starting with Python 3.0 (currently in alpha and scheduled for final release in September), the / operator on ints will return the same as 1.0*x/y . As a transitional measure, this new behavior has been available since Python 2.2 if you start your script with: Code:
from __future__ import division |
All times are GMT -5. The time now is 10:44 PM. |