473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Selecting numbers in a list to be added together

Thekid
145 100+
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=primeList[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?

Expand|Select|Wrap|Line Numbers
  1. def primes(n):
  2.   """ returns a list of prime numbers from 2 to < n """
  3.   if n < 2:  return []
  4.   if n == 2: return [2]
  5.   # do only odd numbers starting at 3
  6.   s = range(3, n, 2)
  7.   # n**0.5 may be slightly faster than math.sqrt(n)
  8.   mroot = n ** 0.5
  9.   half = len(s)
  10.   i = 0
  11.   m = 3
  12.   while m <= mroot:
  13.     if s[i]:
  14.       j = (m * m - 3)//2
  15.       s[j] = 0
  16.       while j < half:
  17.         s[j] = 0
  18.         j += m
  19.     i = i + 1
  20.     m = 2 * i + 3
  21.   # make exception for 2
  22.   return [2]+[x for x in s if x]
  23.  
  24. num = input("Enter number you'd like to begin with:")
  25. primeList = primes(num)
  26. print "List of prime numbers from 2 to < %d:" % num
  27. a=primeList[1::2]
  28. print a
  29.  
Nov 18 '11 #1

✓ answered by Glenton

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.

5 2627
Glenton
391 Expert 256MB
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.
Nov 18 '11 #2
Thekid
145 100+
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.


Expand|Select|Wrap|Line Numbers
  1. for item in a:
  2.     print item
  3.  
That will print each one separately....now just gotta figure out how to do the adding of each 'item'. :) Getting there.
Nov 18 '11 #3
Thekid
145 100+
Actually Glenton....by using the above AND your second suggestion, it seems to work!
Expand|Select|Wrap|Line Numbers
  1. for item in a:
  2.     Sum1stLast= int(str(item)[0]) + int(str(item)[-1])
  3.     print Sum1stLast
  4.  
Now I just need to add together all of the first/lasts so I have 1 total number.
Nov 19 '11 #4
Glenton
391 Expert 256MB
This can be done by adding a mySum = 0 in front of your loop, and then replacing your print statement with mySum+=sum1stLast.

It's also not a bad idea to make the sum1stLast into a function which you can then just call.
Nov 19 '11 #5
Thekid
145 100+
Thank you! Just what I was looking for.
Nov 20 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Charles A. Lackman | last post by:
Hello, I have some textboxes that are doing addition: Dim AString As Double AString = Convert.ToDouble(TextBox1.Text) - Convert.To(DoubleTextBox2.Text) TextBox3.Text =...
7
by: | last post by:
How to call a function with variable argument list from another function again with variable argument list? Example : double average ( int num, ... ); double AFunct1 ( int num, ... ); double...
8
by: janice | last post by:
Hi all, I read a lot of info on this issue, but still don't get it. Why does this happen? document.write(".05" + " \+ " + ".05" + " \= " + (.05 + .05) + "<br>"); document.write("1.05" + "...
3
by: james.dixon | last post by:
Hi I was wondering if anyone else had had this problem before (can't find anything on the web about it). I have three select elements (list boxes - from here on I'll refer to them as 'the...
11
by: Leon | last post by:
I have six textbox controls on my webform that allows the user to enter any numbers from 1 to 25 in any order. However, I would like to sort those numbers from least to greatest before sending them...
3
by: Brent Hoskisson | last post by:
I can't figure out what I'm missing here. I set up a list view control. As I load the form, I select one of the items in the list view control: lvwPrograms.Items(0).Selected = True ...
6
by: Osiris | last post by:
Is the following intuitively feasible in Python: I have an array (I come from C) of identical objects, called sections. These sections have some feature, say a length, measured in mm, which is...
1
by: Selpher | last post by:
Hi guys, I'm having trouble getting these two for loops to interact properly. The point of this program is that a number inputed by the user (we'll use 15 as an example) is supposed to display...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
1
by: libish | last post by:
hi all, can any one suggest me on selcting a list element? i am displaying a list containing some items dynamically... after displaying the content and the form holding this list, i need to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.