Categories
Scripts

Detecting Smith numbers

After learning about Smith numbers today I cooked up this script fairly quickly. There are many places where it could be optimized and cleaned up but that wasn’t really the point – I just needed something that was accurate, and I’m pretty sure this is.

It also helped me to learn about the ulimit command after running it with a HUGE number that had taken up about 7GB of swap before I had to kill the process remotely via SSH…


#!/usr/bin/python
# smith number detector - TWS 1-12-13

def detectSmith(num):
  if(sumFactors(num) == sumDigits(num)):
    return True
  else:
    return False

# calculate the sum of the factors
def sumFactors(num):
  factorList = factors(num)
  sum = 0

  for factor in factorList:
    if factor > 9:
      # if a number (base ten) has multiple digits it has to be broken down
      sum = sum + sumDigits(factor)
    else:
      sum = sum + factor

  return sum

# sum the digits in a number with two or more
def sumDigits(num):
  sum = 0  

  if num > 9:
    while num > 9:
      sum = sum + num % 10
      num = num / 10
    sum = sum + num
    return sum
  else:
    # should never reach this
    return num

def factors(num):
  primeFactorList = []

  factor = lowestPrimeFactor(num)
  currentNum = num

  while factor != -1:
    primeFactorList.append(factor)
    currentNum = currentNum / factor
    factor = lowestPrimeFactor(currentNum)

  if(currentNum != num):
    # preventing returning the number passed in
    primeFactorList.append(currentNum)

  return primeFactorList

def lowestPrimeFactor(num):
  for x in range(2, (num / 2) + 1):
    if(num % x == 0):
      return x
  return -1

for number in range(1, 1000):
  if(detectSmith(number)):
    print "The number", number, "is a Smith number."
Categories
Coding

Ubuntu, PHP, S3, and AWS SDK2 Example

A super quick, super simple little example of how to get started with the AWS SDK2 for PHP. It connects to the S3 service with your credentials and checks to see if a file exists.

  1. Assuming you already have a LAMP setup, you may need to install php-pear:
    sudo apt-get install php-pear
  2. Add the AWS channel and auto discover dependencies:
    pear channel-discover pear.amazonwebservices.com
    pear -D auto_discover=1 install pear.amazonwebservices.com/sdk
  3. Download the .phar file here and throw it on your server in this script’s directory (for this example).
  4. Replace the items in brackets and run the script. The [OBJECT KEY] is just the name of the file, so ‘mypicture.jpg’ in the root of the bucket would have the key ‘mypicture.jpg’.
 <?php

require 'aws.phar';

use Aws\Common\Aws;

$aws = Aws::factory(array(
   'key' => '[KEY HERE]',
   'secret' => '[SECRET HERE]'
));

$client = $aws->get('s3');

echo $client->doesObjectExist('[BUCKET NAME]', '[OBJECT KEY]');

?>

You’ll get back the boolean response of that call (assuming it makes it that far without error). Refer to the API documentation for the other methods and their responses.

Categories
Scripts

A simple Linux content search script

A simple script I use for work to search the contents of files. It just wraps up ‘find’ and ‘grep’ to recursively search and display the location of the file.

#!/bin/bash
# Tanner Stokes - 8-23-12
# Search contents of files

if [ -z "$1" ] || [ -z "$2" ]
then
echo "Usage: csearch \"filetype\" \"content\""
echo "ex: csearch \"*.php\" \"my_function()\""
exit 1
fi

find . -name "$1" -exec grep -Hn "$2" {} \;

Categories
Projects

Clemson vs Ole Miss – Best Tailgaters

Clemson and Ole Miss are currently battling it out in a poll on Southern Living’s website. Out of curiosity I wrote some code to grab this data and graph it to help visualize who is catching up or pulling ahead. Check it out here!

Categories
News

Some derpy numbers

Now that this site has basically turned into ‘herp derp‘ central, I thought I’d post some numbers up regarding downloads. Running this command on my Apache access logs provided me with the download numbers for each browser version (in this case Firefox):

cat access.log | grep xpi | colrm 16 | sort | uniq | wc -l

 

Note: This method isn’t exactly super precise, it would only count once per IP, but it does the job. It also doesn’t account for other sites like download.com.

As of this post:

Chrome: 4722
Firefox: 2564
Safari: 531
Opera: 278

Update: 7-12-12

Shortly after creating this post I got picked up by Boing Boing, Gizmodo, and Reddit. New numbers:

Chrome: 8026
Firefox: 4025
Safari: 919
Opera: 415

Go Herp Derp! 

Categories
News

Herp Derp updated

I got around to fixing some annoying bugs in Herp Derp tonight:

  • When loaded via HTTP Secure (HTTPS) Herp Derp now works (thanks for reminding me @Funshine__Bear)
  • Firefox no longer freaks out when there is only one comment
  • YouTube Feather Mode is now supported (thanks @_sjs)

The latest version is available for all browsers and can be downloaded here.

 

Categories
News

April 2012 Herp Derp update

Herp Derp’s had thousands of downloads! Support for different YouTube modes is coming up, as well as a bug fix or two.

Categories
News

Herp Derp Bug Fixed

I had a minor bug in Herp Derp where the original comments weren’t showing up on pages other than the first – this has been resolved.

Categories
News

Herp Derp for Opera available

Herp Derp now has Opera support!

Categories
News

Herp Derp – Safari support!

It looks like the Safari Developer Certificate stuff got straightened out.

Safari download now available!