shadeofboolek


Joined 5 months ago
Homeworks submitted:
Homework comments:
2
0

About Me

No description provided.

Classes

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming

Class status: Established
Role: Student
. 11% complete

Submitted Assignments

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1

Problem 1 took me a LONG time because I was constantly stuck in infinite loop. After hours and days of thinking, my solution was to print every single numbers (when i run the program) so that I could see where the infinite loop was at. After finding that solution, it took me only a couple more hours to figure out.

Problem 2 took me about 3 hours. This part should've been easy, but I wasn't getting the right answer because I misunderstood the procedure. Just for other's sake I will tell you that the ratio between sum of logs of primes and log of 'n'th number

NOTE After going over others' assignment (to understand part2) I noticed that not everyone had the right answer. I advice others to check if these answers are right before actually using others' work as references

# Problem Set 1 Problem 1
# Name: Boo Park
# Collaborators: none
# Time: 8hours+ couldn't figure out why i was stuck on infinite loop for the longest time.
# 
# Write a program that computes and prints the 1000th prime number.

count = 1 # which prime number the candidate is
candidate = 2 # candidate for prime number
test_num = 2 # number that will be used to test candidate to check for prime number

while count < 1000: # the program will run only until the 1000th prime number

	if candidate % 2 == 0: # check if the candidate is even
		candidate += 1 # make the candidate as an odd number
		print 'candidate: ' + str(candidate)
		
	else: # if the candidate number is odd
		
		while test_num < candidate: # run only while the test number is smaller than the candidate number
			
			if candidate % test_num != 0: # check if the candidate number is divisible by the test_num
				test_num += 1 # change test_num if candidate is not a prime number. 
				
			else: # the candidate number IS divisible by test_num and candidate number is not a prime number
				candidate += 2 # since non-prime number is divisible by test_num, change it
				test_num = 2 # return test_num to 2
		
				
		if test_num == candidate: # we found a prime number
			count += 1 # tell the counter that we found another prime number
			print 'prime number ' + str(count) + ' is ' + str(candidate)
		
			test_num = 2 # return the test_num to 2 in order to find the next prime number
			candidate += 2 # change the candidate number in order to find the next prime number
			
candidate -= 2
print '1000TH PRIME NUMBER IS: ' + str(candidate) # will print the candidate number as prime number when the program is not running anymore

# Problem Set 1 Problem 2
# Name: Boo Park
# Collaborators: none
# Time: 3hours+ didn't understand what the procedure actually meant. found out that the ratio actually meant 'sum of log(primes)' vs 'log('n'th prime).

# NOTE: After I went through others' assignment 1 problem 2, I found out that not everyone had the right answers. If you are using their examples, then make sure that they are right before following their assignments.

# Write a program that computes the sum of the logarithms of all the primes from 2 to some number n, and print out the sum of the logs of the primes, 
# the number n, and the ratio of these two quantities. Test this for different values of n.

import math
from math import *

count = 1 # which prime number the candidate is
candidate = 2 # candidate for prime number
test_num = 2 # number that will be used to test candidate to check for prime number
log_p = math.log(2) # sum of logs of prime numbers
log_n = 0 # log of n
ratio = 0 # ratio of log_p and log_n

n = raw_input('Enter an integer for n') # ask for n
n = float(n) # make n a float so log can be used
	
while count < n: # end at the 'n'th number

	if candidate % 2 == 0: # check if the candidate is even
		candidate += 1 # make the candidate as an odd number
		
	else: # if the candidate number is odd
		
		while test_num < candidate: # run only while the test number is smaller than the candidate number
			
			if candidate % test_num != 0: # check if the candidate number is divisible by the test_num
				test_num += 1 # change test_num if candidate is not a prime number. 
				
			else: # the candidate number IS divisible by test_num and candidate number is not a prime number
				candidate += 2 # since non-prime number is divisible by test_num, change it
				test_num = 2 # return test_num to 2
		
				
		if test_num == candidate: # we found a prime number
			count += 1 # tell the counter that we found another prime number
			candidate = float(candidate) # change candidate to a float so that log() can be used
			log_p += math.log(candidate) # log of candidate added to log_p each time a prime number is found
			
			test_num = 2 # return the test_num to 2 in order to find the next prime number
			candidate += 2 # change the candidate number in order to find the next prime number

nth_p = candidate-2 # find the 'n'th prime number
print 'n: ' + str(n)			
print 'Sum of logs of prime numbers from 2 to n is: ' + str(log_p) # sum of the logs of prime numbers
print 'Ratio of log_p to log_n is: ' + str(log_p/(nth_p)) # ratio between the sum of logs of prime numbers to the log of 'n'th number 

shadeofboolek 5 months ago
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 1, HW 1

I've already started working on this lessons a couple days ago. I've already finished the first assignment and currently working on the second one. I am glad I found this site. I can finally ask people questions and they are (hopefully) willing to answer me with straight answer rather than "read this" and give me a manual :P

# Problem Set 0
# Name: Boo Park
# Collaborators: none
# Time: 0.5 hours
#

lname = raw_input('Enter your last name:')
# will get the user's last name

fname = raw_input('Enter your first name:')
# will get the user's first name

print fname
print lname

shadeofboolek 5 months ago