GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Feb-2010, 08:19
Alphablend's Avatar
Alphablend Alphablend is offline
New Member
 
Join Date: Feb 2010
Location: Bosnia & Herzegovina
Posts: 7
Alphablend will become famous soon enough

Write list of composite numbers as sum of 2 prime numbers


So every composite number can be written as a sum of two prime numbers.

http://en.wikipedia.org/wiki/Composite_number
http://en.wikipedia.org/wiki/Prime_number

Task:
User inputs int n, and also composite numbers which are saved in list which length is n. For every composite number of the list print the sum of two prime numbers.

Solution:
Python Code:
#This function takes one integer as an argument
def prime(x):
    '''Function which returns wheather the number is prime'''
    b=True
    for z in range(x/2):
        if z==0 or z==1:
            z=2
        if x%z==0:
            b=False
            break
        else:
            b=True
    return b

#This one also takes one integer as an argument
def spliter(x):
    '''The function which actually does all the work. It will write the int 
    as 2 prime numbers.'''
    for i in range(x):
        if i==0 or i==1:
            i=3
        if prime(i):
            if prime(x-i):
                return (str)(i)+"+"+(str)(x-i)

#This one takes one array as an argument
def myFunction(a):
    for i in range(len(a)):
        print (str)(a[i])+"="+spliter(a[i])

'''This is function for input of array, which can be used in other programs but with certain modifications'''
def inputArray(array,length):
    for i in range(length):
        k=(int)(raw_input("Enter "+(str)(i+1)+". element of array: "))
        array.append(k)
    return array

array=[]
n=(int)(raw_input("Enter length of array: "))
inputArray(array,n)
myFunction(array)
I know it sounds stupid, but I kind of forgot how do you change from which number does the for loop start. Default is for 0. In this example. I did it my way.
Python Code:
for z in range(x/2):
        if z==0 or z==1:
            z=2
        if x%z==0:
            b=False
            break
        else:
            b=True
    return b
  #2  
Old 23-Feb-2010, 08:45
Alphablend's Avatar
Alphablend Alphablend is offline
New Member
 
Join Date: Feb 2010
Location: Bosnia & Herzegovina
Posts: 7
Alphablend will become famous soon enough

Re: Write list of composite numbers as sum of 2 prime numbers


Oh I almost forgot.
Sample Input/Output
Code:
Enter length of array: 3 Enter 1. element of array: 20 Enter 2. element of array: 18 Enter 3. element of array: 24 20=3+17 18=5+13 24=5+19
  #3  
Old 23-Feb-2010, 17:25
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Write list of composite numbers as sum of 2 prime numbers


Quote:
Originally Posted by Alphablend
...
I know it sounds stupid, but I kind of forgot..

Python Code:
x = 20
for i in range(2, x/2):
    print i

print 'After the "for" loop, i = ', i

Output
Code:
2 3 4 5 6 7 8 9 After the "for" loop, i = 9

Regards,

Dave
  #4  
Old 26-Feb-2010, 16:53
Alphablend's Avatar
Alphablend Alphablend is offline
New Member
 
Join Date: Feb 2010
Location: Bosnia & Herzegovina
Posts: 7
Alphablend will become famous soon enough

Re: Write list of composite numbers as sum of 2 prime numbers


Oh yeah.
Thanks Dave.
Cheers.
  #5  
Old 09-Mar-2010, 04:52
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,671
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Write list of composite numbers as sum of 2 prime numbers


Been learning Python while I wasn't looking, eh Dave?
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.

Interested in pen & paper role playing games from the "golden age" of gaming? Visit Old School Role-Playing Games
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Str_Misaligned in Double Link List Peter_APIIT C Programming Language 1 29-Feb-2008 20:50
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:56.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.