Tumgik
Note
Hello I noticed you are from Missouri, and we are both in the Coursera class: Introduction to Genetics and Evolution....Where in missouri are you from? I am from Blue Springs.
Not really from Missouri, had to come out here because I wasn't making enough in Connecticut(where I am from) to stay, so I'm at my parents trailer park now.  Hopefully I'll get my car fixed soon so I can get a job in town and actually get there.
I'm in rural Buffalo, a few miles outside the city limits.  I have no idea where Blue Springs is, or really where most places are relative to me.  Only been here since August.
0 notes
Text
Primality Testing
Primality testing can be useful for various types of mathematical processes.   So as an early step outside the confines of precisely defined projects with specific pedagogical goals, I decided to write some Python code to test for primality.
I went with brute force, being a simple implementation and sufficiently fast on modern systems for the vast majority of numbers I'll be working with.  I suppose if I were to test something near the limits of a 64 bit integer, a pure brute force approach might be too slow to be practical, but I can cross that bridge when I come to it.
I did make one optimization though.  The most straightforward brute force approach is divide a number by every number up to itself and see if any go in evenly.  This is silly, as anything over half the number obviously doesn't need to be tested.  
Testing everything up to half is, then, the common bare bones brute force method.  However, it's a fact that if no integer below or equal to the square root divides evenly(excepting 1, of course), no number will.  This means fewer potential divisors to check, so assuming you've got a decent sqrt() implementation(you probably do, unless the language is very new or lacks a standard library), it should perform much faster- especially when the number you are testing gets large. So here's my Python code.  
def is_prime(num):  '''(int) -> bool Returns True if number is prime, False otherwise     >>> is_prime(5)     True      >>> is_prime(2)      True     >>> is_prime(9)      False     '''     #Test all possible divisors from 2 - sqrt(number)      max_factor = int(math.sqrt(num))      for i in range(2,max_factor+1):          if num % i == 0:             return False      #Checked all possible divisors, none worked     return True
0 notes
Text
Learning Perl
In addition to the Java(which I'll be getting back to, eventually), I occasionally play around with Perl.  The book I'm using is Learning Perl, 3rd Edition, it's been updated a couple times since I got it but I haven't picked up the newer edition yet.
Here's an answer to Chapter 2, Exercise 1.  The problem is to calculate the circumference of a circle with a radius of 12.5.  Units aren't given, but since there's no comparison with anything else, they can be safely omitted without affecting the outcome.
#!/usr/bin/perl - w $pi = 3.141592654; $radius = 12.5; $circumference = 2*$radius*$pi; print "$circumference\n";
Note the use of descriptive variables.  Pi should be a constant, yes, but Perl doesn't support constants in the way most languages do, and I'd rather not clutter this early exercise with the implications of the Perl way.  
Anyways, with Pi as a named value of some sort, this lets us change Pi.  While the value doesn't change, we might want to change the precision.  Also, it lets us know that we wanted Pi in the program, not some other number that happened to equal it out to a given degree of precision.
Potential changes:
-Put the calculation in a function
-Make $pi a constant.
-Have the value of $radius input by the user, rather than assigned directly in code.
1 note · View note
Text
Metro App Development
I've been wanting to get into Metro app development for Windows 8 on the ground floor.  I could make some money with relatively simple apps, and if I come up with something good(dead simple can be good, if it does some useful task in a complete way), I might get enough of an early reputation to have some ongoing income after the bigger players get well established. I was thinking just a few minutes ago, wouldn't it be cool to have OS X dev tools?  Then it hit me- there are.
One of the options for creating Metro apps is HTML/CSS/JavaScript.  This can be done in any text editor, and can be tested in any web browser.  While final testing and tweaking would need to be done on a Windows 8 device, the majority of development can be done with tools I'm already familiar with, on my preferred OS. 
Another advantage to the HTML/CSS/JS style of Metro apps, is this is how websites are done.   I want a website version of my app, or an app version of some website, this lets me easily share most, and in some cases, all of the code.  Well, all the computational and data management stuff, UI controls will likely always need to be modified.  Such a web app version of the Metro app could also reach Android and iOS devices, and other handheld platforms. Thinking about it, that's damn clever.  Metro apps are likely to be more easily ported to other platforms than either iOS or Android apps- all modern consumer, and most business, platforms have web browsers.  People start using a Metro app, they like it, they use a few more... and then they say "hey, these are all fundamentally Windows 8 apps, you know what, I might as well switch phones to get a better experience with the apps I use".  I'm not sure if this is their plan, but if they can get devs to release webapp versions that other platforms can access, that might get them some switchers.
0 notes
Text
Consonant Detector
As part of the class, I'm working through several of the exercises in the textbook.  They aren't assignments, but it's good to do a bit of extra work.  This one is to write a method to determine if a character is an English consonant.  Chapter 9, Exercise 1, "The Art and Science of Java". Design concerns- What if the char value you are testing is not a letter?  You could rely on clients to only feed it letters, but that seems like a bad idea.  Someone would forget to test the data, and the data would end up being non-alphabetic.  
Now, there's two ways to handle this that I see.  You can test your data internally to the method, or you can test for consonants specifically.
Internal data testing is good.  You can't control the data being passed.  Doing things like calling Character.isLetter(ch) gets you in a good habit for when you don't have such an easily defined domain to test for as English consonants. 
With that done, you don't need to directly test for consonants.  You can test for vowels, and return false if those tests succeed.  5 tests, vs 21 tests.  While premature optimization is generally questionable, here, you're doing the same exact thing.  Just a lot less of it.
/* ConsonantDetector.java * Program to test method isEnglishConsonant(ch), which determines if the * passed character is a consonant in the English language. **/ import acm.program.*; public class ConsonantDetector extends ConsoleProgram{ public void run(){ for(char i = 'A'; i <= 'Z'; i++){ if(isEnglishConsonant(i)){ println(i); } } } /** Determines if the passed character is a consonant in the English alphabet. * While 'y' sometimes fills the role of a vowel, this function assumes it is * a consonant. Results will be unpredictable if passed a valid letter from * another language * @param ch character to be tested * @return true if letter is a consonant; false if it is not*/ private boolean isEnglishConsonant(char ch){ /*Return false if ch does not hold a letter*/ if(!Character.isLetter(ch)){ return false; } /*There are fewer values for vowels, so we test for and reject those, * returning true if none are rejected*/ switch(ch){ case 'A': case 'a': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u': return false; default: return true; } } }
4 notes · View notes
Text
GCircle extends GPolygon
So, an offhand comment from the instructor had me thinking about creating a GCircle class that had its location coordinates centered, rather than the upper left of the bounding rectangle.
Two ways to do this.  Extend GOval, and mess with the location coordinates, or extend GPolygon and draw an arc.  I went with the latter, it seemed like a cleaner implementation at the time.  Clients of the class might want to assign GCircles to GOvals, so I might revisit this and do it by extending GOval.  Anyways, here's my code.  Requires the ACM library.
import acm.graphics.*; /** The GCircle class is a graphical object whose appearance consists of a circle*/ public class GCircle extends GPolygon implements GResizable{ /** Constructs a new circle of a given radius at the origin*/ GCircle(double radius){ super(); this.radiusX = radius; this.radiusY = radius; drawCircle(); } /** * Constructs a new circle of a given radius at(x,y)*/ GCircle(double centerXPos, double centerYPos, double radius){ super(centerXPos, centerYPos); this.radiusX = radius; this.radiusY = radius; drawCircle(); } /*Utility function to hold the common bits of the constructors*/ private void drawCircle(){ double diameter = radiusX * 2; super.addVertex(0, -radiusX); super.addArc(diameter, diameter, 90, 360); } /** * Constructs a GRect corresponding to the smallest square that will fit the circle*/ public GRect getBoundingBox(){ //this.recenter(); GPoint p = this.getLocation(); return new GRect(p.getX()- radiusX, p.getY()- radiusY, 2*radiusX, 2*radiusY); } public void scale(double sx, double sy){ super.scale(sx, sy); radiusX *= sx; radiusY *= sy; } double radiusX; double radiusY; /*This section implements the methods in GResizable, plus a utility method * for their use*/ /** * Sets the bounds. */ public void setBounds(double x, double y, double width, double height){ double newRadiusX = width/2; double newRadiusY = height/2; scaleToNewRadii(newRadiusX, newRadiusY); super.setLocation(x, y); } public void setBounds(GRectangle bounds){ double newRadiusX = bounds.getWidth()/2; double newRadiusY = bounds.getHeight()/2; scaleToNewRadii(newRadiusX, newRadiusY); super.setLocation(bounds.getX() + newRadiusX, bounds.getY() + newRadiusY); } public void setSize(double width, double height){ double newRadiusX = width/2; double newRadiusY = width/2; scaleToNewRadii(newRadiusX, newRadiusY); } public void setSize(GDimension size){ double newRadiusX = size.getWidth()/2; double newRadiusY = size.getHeight()/2; scaleToNewRadii(newRadiusX, newRadiusY); } private void scaleToNewRadii(double newRadiusX, double newRadiusY){ double scaleFactorX = newRadiusX/radiusX; double scaleFactorY = newRadiusY/radiusY; radiusX = newRadiusX; radiusY = newRadiusY; super.scale(scaleFactorX, scaleFactorY); } }
2 notes · View notes