Thursday, April 14, 2005

a trip into the mind of a computer programmer

in choosing my major as computer engineering, i've finally gotten to learn things about computers i've never known. . .mainly coding. now the coding i'm doing is nowhere near that complex or intricate as real programming that runs most operating systems, i'll get to that down the road, but i am getting an introduction into the main ideas such as basic syntax, modularity, strings, and graphic programming.

it kind of sucks because most everyone in my class has previous experience with programming because they all had computer classes in their "big town" high schools. me, nilch. i guess i didn't do any exploring myself either, but still. i was busy being a popular jock, i didn't have time for being a nerdy computer nerd. and i seem to be learning well. a funny thing is, i bet i have just as high of a grade in that class, if not higher than some of the other nerdy nerds.

the programming language we learn in is called python. it's a lot simpler than say java or C++ or other languages. there's a lot less syntax, so you have to type a lot less for basic operations. what i submit after this paragraph is a little gem of mine that i take some pride in. the problem laid forth to us was this. . . .
according to the Goldbach Conjecture every even number is the sum of two prime numbers. i had to type a program where a user inputs an even number and the program finds two prime numbers that sum to the input number.
now this program had to have catchalls, basically if the program doesn't work properly or an input is wrong, it has to perform another task instead of letting the program put out an error message, so you really have to cover all your bases. and just so it is noted, all type following the # are comments that will tell you what certain lines do.

behold. . . . . .

import math

def prime(num):
divisor=2
#initialize divisor variable
while num%divisor!=0 and divisor<=math.sqrt(num):
#if either condition comes back false, while loop is exited
divisor=divisor+1
#if while loop is entered, variable is changed
if num%divisor!=0:
#after while loop is exited, checks current value of divisor to see if number is prime
return "prime"

def primeif(num, number1, number2):
if prime(number1)=="prime" and prime(number2)=="prime":
#check to see both are prime. if so, print statement
print "The two prime numbers that sum to %0d are %0d and %0d." %(num, number1, number2)

def main():
print "According to the Goldbach conjecture, every even number is the sum"
print "of two prime numbers."
print "This program calculates two prime numbers that sum to your input number."
#print program headers
number=input("Enter an even number: ")
#user inputs even number to calculate
if number%2==0:
#checks that input value is even
num1=3
num2=number-num1
#initializes variables num1 and num2 so they sum to input number
primeif(number, num1, num2)
while prime(num1)!="prime" or prime(num2)!="prime":
#runs prime function for both num1 and num2
#both conditions must be false to exit loop
num1=num1+1
num2=number-num1
#re-initializes num1 and num2 if still in loop
primeif(number, num1, num2)
else:
#print statement when input number is not even
print "The number is not even."
main()

now the ouput of this problem would look something like. . . .

According to the Goldbach conjecture, every even number is the sum
of two prime numbers.
This program calculates two prime numbers that sum to your input number.
Enter an even number: 30
The two prime numbers that sum to 30 are 7 and 23.

well, now that you've been bored to death, i'm gonna prevent myself from getting carpal tunnel.

2 Comments:

Blogger Scotteth too hotteth said...

damn shit took out all my indentation. screw you blog website with your hidden formatting. just so everyone that views the comments notes, that isn't how the typed out program actually looks. but i guess you get the gyst.

10:36 PM  
Blogger Logan Clark said...

damn, the loss of indents ruined it for me. you fat russian bastards!

7:59 AM  

Post a Comment

<< Home