#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Is my code for a hwk assigment right?


    Code:
    http://i.imgur.com/LgTvLbT.jpg

    a_num=raw_input("Enter an integer for a:")
    b_num=raw_input("Enter an integer for b:")
    a_num=int(a_num)
    b_num=int(b_num) sum=0
    a_num=raw_input("Please enter an integer < b:")
    a_num=int(a_num) print "Multiples of A that are less than B are:" for i in range(0,b_num,a_num):
    print i sum=i+sum
    print "Sum of these multiples:",sum
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Originally Posted by lunapt
    Code:
    http://i.imgur.com/LgTvLbT.jpg

    a_num=raw_input("Enter an integer for a:")
    b_num=raw_input("Enter an integer for b:")
    a_num=int(a_num)
    b_num=int(b_num) sum=0
    a_num=raw_input("Please enter an integer < b:")
    a_num=int(a_num) print "Multiples of A that are less than B are:" for i in range(0,b_num,a_num):
    print i sum=i+sum
    print "Sum of these multiples:",sum
    Which part of this code finds out the multiples and prints them?
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    With new lines and indentations in the right places your program works. Why do you get a value for a_num twice?
    Try to find a different algorithm. Removing your intermediate print statements, and setting

    b_num = 20000000
    a_num = 2

    my algorithm, O(1), runs sixty thousand times faster than your O(n) algorithm.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo