During the protests regarding SOPA/PIPA, I put my name down on a few lists to contact my Senators and Representatives to let them know my feelings on the matter. I have now received the scripted replies to those notes and feel like I ought to share them here.
I came across an article on Hacker News with an interesting title – “Why programmers are not paid in proportion to their productivity”. Should be an interesting read, I thought. Besides, this guy always has good articles.
So I clicked on the link. And instead of a nice article, I got the following message instead:
I thought that text could be considered highly appropriate. What do you think? (By the way, there actually is an article there. This was just a fluke)
Problem 10
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Script
Execution Time: 22.5509 seconds
Changes
Basically, all math functions went into a class first off. Then I basically had the array populated and removed multiples of each number in the array starting with lowest value and going up from there.
I did run into an issue where I ran out of memory. This was solved by not including either even numbers or those divisible by 3 in the array. So the first number I worked with was 5 (4 was skipped because it was an even number.
I removed each multiple of 5 from the array of primes leaving 5 in the array. Once that was complete, I went on to the next value (7) and performed the same operations.
Doing the calculations in this manner saved approximately a full minute off the brute force method. I am sure there is an even faster way to calculate the value, but I am happy for now with the 22 seconds.
class.mathFunctions.php
class MathFunctions
{
public $v;
private $arraySum;
public function __construct()
{
$this->v = array();
$this->arraySum = 0;
}
/**
* isPrime
*
* @param integer $n
* @access public
* @return void
*/
public function isPrime($n)
{
for ($x=2; $x<=sqrt($n); $x++) {
if ($n%$x == 0) {
return false;
}
}
return true;
}
public function getPrimesBetween($a, $b)
{
if ($a == 1) $a++;
if ($a == 2) {
$this->v[$a] = 0;
$a++;
}
if ($a == 3) {
$this->v[$a] = 0;
$a++;
}
for($x = $a; $x <= $b; $x++) {
if ($x%2 != 0 && $x%3 != 0) {
$this->v[$x] = 0;
}
}
for($x = $a; $x <= $b/2; $x++) {
$this->_removeNonPrimes($x);
}
}
private function _removeNonPrimes($step)
{
$end = end($this->v);
$end = key($this->v);
for($x=$step*2; $x <= $end; $x += $step) {
unset($this->v[$x]);
}
}
public function getSumOfPrimesBetween($a, $b)
{
$this->getPrimesBetween($a, $b);
array_walk($this->v, array($this, '_addArrayKeyValues'));
return $this->arraySum;
}
private function _addArrayKeyValues($value, $key)
{
$this->arraySum += $key;
}
/**
* isPandigital
*
* @param integer $n
* @access public
* @return boolean
*/
public function isPandigital($n)
{
if (is_array($n) === false) $n = str_split($n);
$x = range(1, count($n));
$y = array_unique($n);
sort($y);
if (($x == $y) && (in_array(0, $x) === false)) {
return true;
} else {
return false;
}
}
/**
* isPalindrome
*
* @param mixed $s
* @access public
* @return void
*/
public function isPalindrome($s)
{
if (is_array($s) === false) $s=str_split($s);
$t = array_reverse($s);
if ($s == $t)
return true;
else
return false;
}
/**
* isLychrel
*
* @param int $n
* @param int $c
* @access public
* @return void
*/
function isLychrel($n, $c=0)
{
$x = str_split($n);
$x = array_reverse($x);
$x = join('', $x);
$s = bcadd($x, $n);
if ($this->isPalindrome($s) === true) {
return false;
} elseif($c >= 50) {
return true;
}
return $this->isLychrel($s, ++$c);
}
}
/**
* This is for the Form Builder class
*
* PHP Version 5.2
*
* @category Base_Classes
* @package FormBuilder
* @author Joseph Thayne
* @license http://www.google.com Unknown License
* @link http://www.mythoughtexactly.com
*/
/**
* FormBuilder class
*
* @category Base_Classes
* @package FormBuilder
* @author Joseph Thayne
* @copyright 2011 Joseph Thayne
* @license http://www.google.com Unknown License
* @version Release: 2.0
* @link http://www.mythoughtexactly.com
*/
class FormBuilder
{
private $form;
/**
* __construct
*
* @access protected
* @return void
*/
function __construct()
{
}
/**
* _defineAttribute
*
* @param mixed $name The name of the attribute
* @param mixed $value The value of the attribute
*
* @access private
* @return string
*/
private function _defineAttribute($name, $value)
{
if ($value === null) {
$output = "";
} else {
$output = " $name='$value'";
}
return $output;
}
/**
* beginForm Open a form with the appropriate attributes
*
* @param string $name The name of the form
* @param string $action The url the form will be submitted to
* @param string $method The method the form will be submitted.
* Options are GET and POST
* @param array $attributes Array holding potential attributes for the form
* element. Possible values are id, class,
* enctype, and style
*
* @access public
* @return string
*/
function beginForm($name, $action, $method="POST", $attributes = array())
{
/**
* Possible attributes for $attributes are:
* id, class, enctype, and style
*/
$attArray = array();
foreach ($attributes as $name=>$value) {
$attArray[] = $this->_defineAttribute($name, $value);
}
$att = join(' ', $attArray);
$output = "
".$followingText.PHP_EOL;
return $this;
}
/**
* createLabel Creates a label tag for an associated form element
*
* @param mixed $value The text of the label
* @param mixed $name The name of the element the label refers to
* @param mixed $prefix Text that will display just before the label tag
* @param mixed $suffix Text that will display just after the label tag
*
* @access public
* @return string
*/
function createLabel($value, $name, $prefix=null, $suffix=null)
{
$this->form .= $prefix."".$suffix.PHP_EOL;
return $this;
}
/**
* createTextBox Creates a text box form element
*
* @param string $name The name of the text box
* @param array $attributes Array holding potential attributes for the
* form element. Possible values are value, id,
* class, size, maxlength, and style
*
* @access public
* @return string
*/
function createTextBox($name, $attributes = array())
{
/**
* Possible attributes for $attributes are:
* value, id, class, size, maxlength, and style
*/
$attArray = array();
foreach ($attributes as $name=>$value) {
$attArray[] = $this->_defineAttribute($name, $value);
}
$att = join(' ', $attArray);
$output = "
".PHP_EOL;
$this->form .= $output;
return $this;
}
/**
* createTextBoxWithLabel Creates a text box form element with associated label tag
*
* @param string $label The text of the label
* @param string $name The name of the text box
* @param array $attributes Array holding potential attributes for the
* element. Possible values are value, id,
* class, size, maxlength, and style
* @param string $labelPrefix Text that will display just before the
* label tag
* @param string $labelSuffix Text that will display just after the
* label tag
*
* @access public
* @return string
*/
function createTextBoxWithLabel($label, $name, $attributes = array(), $labelPrefix=null, $labelSuffix=null)
{
/**
* Possible attributes for $attributes are:
* value, id, class, size, maxlength, and style
*/
$this->createLabel($label, $name, $labelPrefix, $labelSuffix)
->createTextBox($name, $attributes);
return $this;
}
/**
* createPasswordBox Creates a password box form element
*
* @param string $name The name of the passwordbox
* @param array $attributes Array holding potential
* attributes for the element.
* Possible values are id, class,
* size, maxlength, and style
* @param boolean $showPasswordWhileTyping Display the password while
* typing instead of masking it
*
* @access public
* @return string
*/
function createPasswordBox($name, $attributes, $showPasswordWhileTyping=false)
{
/**
* Possible attributes for $attributes are:
* id, class, size, maxlength, and style
*/
$showPasswordWhileTyping = (bool)$showPasswordWhileTyping;
if ($showPasswordWhileTyping === true) {
$this->createTextBox($name, "", $attributes);
} else {
$attArray = array();
foreach ($attributes as $name=>$value) {
$attArray[] = $this->_defineAttribute($name, $value);
}
$att = join(' ', $attArray);
$output = "
".PHP_EOL;
$this->form .= $output;
}
return $this;
}
/**
* createPasswordBoxWithLabel Creates a password box form element with associated label tag
*
* @param string $label The text of the label
* @param string $name The name of the passwordbox
* @param array $attributes Array holding potential
* attributes for the element.
* Possible values are id, class,
* size, maxlength, and style
* @param int $showPasswordWhileTyping Display the password while typing
* instead of masking it
* @param string $labelPrefix Text that will display just
* before the label tag
* @param string $labelSuffix Text that will display just
* after the label tag
*
* @access public
* @return string
*/
function createPasswordBoxWithLabel($label, $name, $attributes=array(), $showPasswordWhileTyping=0, $labelPrefix=null, $labelSuffix=null)
{
$this->createLabel($label, $name, $labelPrefix, $labelSuffix)
->createPasswordBox($name, $attributes, $showPasswordWhileTyping);
return $this;
}
/**
* createCheckBox Creates a checkbox form element
*
* @param string $name The name of the check box
* @param string $value The value of the field
* @param boolean $checked Is the check box checked by default
* @param array $attributes Array holding potential attributes for the
* element. Possible values are id, class,
* and style
*
* @access public
* @return string
*/
function createCheckBox($name, $value, $textPreceeding='', $textFollowing='', $checked=false, $attributes=array())
{
$attArray = array();
foreach ($attributes as $name=>$value) {
$attArray[] = $this->_defineAttribute($name, $value);
}
$att = join(' ', $attArray);
$ckText = '';
if ($checked === true) {
$ckText = " checked='checked'";
}
$output = "$textPreceeding
$textFollowing";
$this->form .= $output;
return $this;
}
/**
* createRadioButton Creates a radio button form element
*
* @param string $name The name of the radio button
* @param string $value The value of the field
* @param string $text The text displaying at the end of the field
* @param boolean $checked Is the radio button selected by default
* @param array $attributes Array holding potential attributes for the
* element. Possible values are id, class,
* and style
*
* @access public
* @return string
*/
function createRadioButton($name, $value, $textPreceeding='', $textFollowing='', $checked=false, $attributes=array())
{
$attArray = array();
foreach ($attributes as $name=>$value) {
$attArray[] = $this->_defineAttribute($name, $value);
}
$att = join(' ', $attArray);
$ckText = '';
if ($checked === true) {
$ckText = " checked='checked'";
}
$output = "$textPreceeding
$textFollowing";
$this->form .= $output;
return $this;
}
/**
* echoOut Print the generated form and clear the register.
*
* @access public
* @return void
*/
function echoOut()
{
echo $this->form;
$this->form = '';
}
}
Problem 12
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …
Let us list the factors of the first seven triangle numbers: