About Me
No description provided.
Classes
|
Class status: Established
Role:
Student
|
.
17% complete
|
|
Class status: Established
Role:
Student
|
.
6% complete
|
Submitted Assignments
 |
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1
#!/usr/bin/env python
#
# Problem Set 1a
#
# A program that computes and prints the 1000th prime number.
# Finds primes using trial division (least efficient method)
#------------------------------------------------------------
prime_count = 0
n = 2
while (prime_count <= 1000):
#if even, check for 2, the only even prime
if (n % 2 == 0):
if n == 2:
prime_count += 1
n += 1
else:
# number is odd, possible prime
for div in range(3, n, 2):
if (n % div == 0):
# not a prime
n += 1
break
else:
# prime!
prime_count += 1
if prime_count == 1000:
print "The 1000 prime is", n
else:
n += 1
#!/usr/bin/env python
#
# Problem Set 1b
# A program that computes the sum of the logarithms of all the
# primes from 2 to n, prints out the sum of logs, n, and the
# ratio of both.
#
#------------------------------------------------------------
from math import log
logs = 0
limit = input("Enter a number: ")
limit += 1
for n in range(2, limit):
#if even, check for 2, the only even prime
if (n % 2 == 0):
if n == 2:
logs += log(n)
else:
# number is odd, possible prime
for div in range(3, n, 2):
if (n % div == 0):
# not a prime
break
else:
# prime!
logs += log(n)
print "Sum of logs of the primes = ", logs
print "n = ", n
print "ratio = ", (float(logs)/float(n))
fell
1 year ago
|
 |
Programming in C: Lesson 1, HW 3
A slight modification to the program given in the book resulted in this answer.
/* Exercise 1-5
*
* Description:
* Modify the temp conversion program to print the table in reverse
* order, that is, from 300 degrees to 0.
*
* Date:
* 03/31/2011
*
*/
#include <stdio.h>
int main(void)
{
int fahr;
for (fahr = 300; fahr >= 0; fahr = fahr - 20)
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}
fell
1 year ago
|
 |
Programming in C: Lesson 1, HW 2
Using "while" loops only and no functions it's kind of dirty but it works.
/* Exercise 1-3
*
* Description:
* Modify the temperature conversion program to print a heading above
* the table.
*
* Date: 03/29/2011
*
*/
#include <stdio.h>
int main(void)
{
float fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
// print header
int count = 0;
while (count < 25)
{
// print asterisk
putchar('*');
count++;
}
putchar('\n');
// print program name
printf("Temperature Conversion\n");
// print a divder
count = 0;
while (count < 25)
{
putchar('-');
count++;
}
putchar('\n');
printf("Fahrenheit\tCelsius\n");
count=0;
while (count < 25)
{
// print asterisk
putchar('*');
count++;
}
putchar('\n');
fahr = lower;
while (fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
printf("%3.0f\t\t%6.1f\n", fahr, celsius);
fahr = fahr + step;
}
}
/* Exercise 1-4
*
* Description:
* Write a program to print the corresponding Celsius to Fahrenheit table.
*
* Date: 03/29/2011
*
*/
#include <stdio.h>
int main(void)
{
float fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
// print header
int count = 0;
while (count < 25)
{
// print asterisk
putchar('*');
count++;
}
putchar('\n');
// print program name
printf("Temperature Conversion\n");
// print a divder
count = 0;
while (count < 25)
{
putchar('-');
count++;
}
putchar('\n');
printf("Celsius\tFahrenheit\n");
count=0;
while (count < 25)
{
// print asterisk
putchar('*');
count++;
}
putchar('\n');
fahr = lower;
while (fahr <= upper) {
fahr = (celsius * (9/5)) + 32;
printf("%3.0f\t\t%6.1f\n", celsius, fahr);
celsius = celsius + step;
}
}
fell
1 year ago
|
 |
Programming in C: Lesson 1, HW 1
Successfully ran the Hello World program on Debian Lenny using gcc 4.4.5.
/* Programming in C
* Exercise 1-1
*
* Description:
* prints "Hello, World!" on the screen
*
* Date: 03/28/2011
*
*/
#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
}
fell
1 year ago
|
 |
|
|
|