LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-30-2008, 03:11 PM   #1
raskol
Member
 
Registered: Mar 2008
Posts: 51

Rep: Reputation: 15
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: ")
a=int(a)
b=0
while a!=1:
    if a==a/2*2:
        a=a/2.0
        print a
    else:
        a=a*3+1
        print a
    b=b+1

print b

raw_input()
i get b=1076 when doing it on 6. but it seems u will always divide until it becomesso small that maybe the program does something weird?

also in the else-part do i need to do a=a*3.0+1 or is it ok with just 3?

Last edited by raskol; 03-30-2008 at 04:16 PM.
 
Old 03-30-2008, 04:01 PM   #2
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
[ 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.
 
Old 03-30-2008, 04:06 PM   #3
Maligree
Member
 
Registered: Mar 2008
Distribution: Gentoo, CentOS, Fedora, Arch
Posts: 231
Blog Entries: 1

Rep: Reputation: 42
Code:
a=raw_input("what's the number, master? ")
a=int(a)
b=0

while a!=1:
        if a%2==0:
                print str(a)+" (even)"
                a=(a/2)
        else:
                print str(a)+" (odd)"
                a=a*3+1
        b+=1

print "reached 1 after "+str(b)+" loops"
Worked for me.

(Wow, it's been a while since the last time I wrote anything in Python. I forgot how beautiful it is..)

Last edited by Maligree; 03-30-2008 at 04:08 PM. Reason: Got rid of the 'b=int(b)' statement: useless.
 
Old 03-30-2008, 04:15 PM   #4
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Use the // operator instead of / if you want to do int division.
 
Old 03-30-2008, 04:19 PM   #5
raskol
Member
 
Registered: Mar 2008
Posts: 51

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Dan04 View Post
Use the // operator instead of / if you want to do int division.
a//2*2 is the same as a/2*2 no?
 
Old 03-30-2008, 08:04 PM   #6
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
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):
    return sum(num_list) / len(num_list)
mean([3.0, 4.0]) == 3.5, but mean([3, 4]) == 3. A lot of people complained about this. So Python decided to have two distinct division operators: / and //.

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
Always do this. It's less error prone, and will make the transition to 3.0 easier.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Fedora 2 04-23-2009 07:02 PM
LXer: Charming Python: Python elegance and warts, Part 2 LXer Syndicated Linux News 0 08-31-2007 08:40 AM
python, os.system() function. howto use python variables? jhwilliams Programming 5 07-28-2007 01:56 AM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM
python problem - compiled from source - python -V still showing old version txm123 Linux - Newbie 1 02-15-2006 11:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration