Selecting numbers in a list to be added together

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thekid
    New Member
    • Feb 2007
    • 145

    Selecting numbers in a list to be added together

    Hi. I am trying to enter a number into this script and have it do several things. 1st I want it to find all of the prime numbers from the one that's entered by the user. I found some code online that does that (below). Then I want only every other prime in the list, which I've managed with "a=primeLis t[1::2]". I'm not sure how to go about the next part which will be to take the first number of a multi-digit prime and add it to the last number of that prime and do it for all primes in the list. Example:
    Enter number to begin with:125
    List of prime numbers from 2 to < 125:
    [113, 107, 101, 89, 79, 71, 61, 53, 43, 37, 29, 19, 13, 7, 3]
    1+3, 1+7, 1+1, 8+9, 7+9....etc (single digits get added to themselves so for 7,3 it would be 7+7, 3+3)

    How do I go through my list of every other prime and get those particular numbers to add?

    Code:
    def primes(n):
      """ returns a list of prime numbers from 2 to < n """
      if n < 2:  return []
      if n == 2: return [2]
      # do only odd numbers starting at 3
      s = range(3, n, 2)
      # n**0.5 may be slightly faster than math.sqrt(n)
      mroot = n ** 0.5
      half = len(s)
      i = 0
      m = 3
      while m <= mroot:
        if s[i]:
          j = (m * m - 3)//2
          s[j] = 0
          while j < half:
            s[j] = 0
            j += m
        i = i + 1
        m = 2 * i + 3
      # make exception for 2
      return [2]+[x for x in s if x]
    
    num = input("Enter number you'd like to begin with:")
    primeList = primes(num)
    print "List of prime numbers from 2 to < %d:" % num
    a=primeList[1::2]
    print a
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Hi
    Thanks for posting. Here's two ways to do this:
    1. Mathematical: the last digit is given by num%10. The first is given by dividing the number by the biggest power of 10 that's smaller than the number (assuming python 2.6 or lower where dividing integers returns an integer). Taking the log base ten of the number and rounding down and then adding 1 gives the power.

    2. Sneaky conversions: convert the integer to a string (s=str(num)), and then use string operations to get the first and last digit. Then convert back. In one line, assuming your prime is p:
    Sum1stLast= int(str(p)[0]) + int(str(p)[-1])

    I haven't checked this, and obviously type(p) must be int, but hopefully this helps.

    Comment

    • Thekid
      New Member
      • Feb 2007
      • 145

      #3
      Thanks for the reply. I was thinking about trying to separate each prime and then try to implement the adding of the first & last numbers.


      Code:
      for item in a:
          print item
      That will print each one separately....n ow just gotta figure out how to do the adding of each 'item'. :) Getting there.
      Last edited by Thekid; Nov 19 '11, 02:34 AM. Reason: Added code snippet as ideas are coming in.

      Comment

      • Thekid
        New Member
        • Feb 2007
        • 145

        #4
        Actually Glenton....by using the above AND your second suggestion, it seems to work!
        Code:
        for item in a:
            Sum1stLast= int(str(item)[0]) + int(str(item)[-1])
            print Sum1stLast
        Now I just need to add together all of the first/lasts so I have 1 total number.

        Comment

        • Glenton
          Recognized Expert Contributor
          • Nov 2008
          • 391

          #5
          This can be done by adding a mySum = 0 in front of your loop, and then replacing your print statement with mySum+=sum1stLa st.

          It's also not a bad idea to make the sum1stLast into a function which you can then just call.

          Comment

          • Thekid
            New Member
            • Feb 2007
            • 145

            #6
            Thank you! Just what I was looking for.

            Comment

            Working...