Saturday, July 26, 2014

The Time Of Primes


I read a while ago that somebody had found a new prime number. I didn't think too much of it at first, apart from the question of why would anybody still be doing this.

Apparently I'm not the only person. When the story first came up, a friend at work asked me (probably because I have a degree in Math and he thought I'd know this kind of thing) if this new prime number was going to be able to do anything special. After some googling and admissions of how uninformed I was, I told him that primes can be useful for cryptography, but only smaller primes (100-200 digits).

Apart from that, I just wondered how easily I could create a program to calculate primes. Well, the answer was pretty easily. See for yourself -

while (true)
{
  if (numberIsPrime(number))
  {
    Console.WriteLine(number.ToString());
  }
  number += 1;
}

private static bool numberIsPrime(int number)
{
  // Loop through all the numbers from 1 - number
  for (int numberCount = 1; numberCount <= number; numberCount++)
  {
    // If the modulus of the number is 0, it means it divided without a remainder
    if (number % numberCount == 0)
    {
      // The number can divide itself and 1 and still be a prime
      if (numberCount != 1 && numberCount != number) return false;
    }
  }
  // If we got this far, return true, it's a prime
  return true;
}
So that was it for a while. I didn't think about it for 6 months or so.

Then for some reason it popped back into my mind. I think when I read the story about the new prime I did check the number, but it obviously didn't register with me. This time I went back to the story and thought about the actual prime number. Well, in case you're interested, the number is 257,885,161− 1. That actually works out at being a number with 17 million digits (17,425,170 to be exact, and why shouldn't we be when we're working with primes).

It's hard to visualize a number that long with any real clarity, so I'm going to try and give you something to compare it to. I thought back to my Physics days and some of the huge numbers we dealt with. The number of atoms in the universe (the whole universe) is estimated to be a number of only 80 digits. OK, not even close, let's go bigger, something stupid like star distances. Well the "width" of the universe is a number with only approximately 30 digits.

Clearly nothing else is on the scale of prime number calculations.

OK, so even if we ignore the very scale of the number, I was intrigued as to how long it might take to calculate a number like that using a regular computer. Now regular here, is in regards to super computers. I have an Intel Core i7, with 8Gb of RAM, so my laptop is top of the line.

Currently, all the latest top prime numbers are being discovered by a distributed internet project known as the Great Internet Mersenne Project. Here's a list of the numbers they've found -


Now this is not an official record of time spent, but according to this list it took them close to 4 years to find this latest prime number. At first glance, that seems crazy, but after spending a little time finding primes I'm not so sure.

Yes, the final step of this investigation (can you call one guy messing around an investigation?) is me finding out how long it would take one person to find this prime number. Obviously, I'm not using parallel processing, so in theory it's going to take more than 4 years, and I don't want to wait over 4 years to blog about these results, so I used a little math and some good old excel graphs to get a rough idea.

First, I modified my program above to log the time taken to find each prime number, and then let it go for an afternoon. The largest prime I calculated was 5590853 (only 7 digits) and on the way I found 3866 other primes. It took me 3 hours and 13 minutes to find this prime.

Using this data I calculated a best line graph with the following equation -
y = 9.385552520679E-08e1.133509190066E+00x
Comparing this equation to the actual results was only an error of 12.5%. Admittedly, not great, but good enough for our purposes. I then tried to use this equation to calculate how long it would take to find the largest prime number, but excel couldn't handle it. The largest it could get to was 2^600 (keep in mind the largest prime is 2^57,000,000). It would take 2.6*10125 seconds. That's a number with 125 digits in it, already bigger than the number of atoms in the universe. That's more time than the age of the universe. A lot more time.

Once I realized this, there really was nowhere else to go with it. Obviously, a single person calculating prime numbers is now completely ridiculous.

I hate to go out on a low, but this was really the end of my investigation. It turns our when you get to this scale, everything is too big to comprehend. Every number is hypothetical, as nothing exists in the universe in a quantity that even gets close to these primes.

At the start of this post, I talked about why would anybody even bother calculating primes anymore, but then I spent hours of my time calculating how long it would take to calculate the primes. I'm worse than they are, but yet in doing all this, I kind of discovered why people are probably still doing this. It's pure human interest. What does it take to find the next one, and how long will it take? The last one took 4, but could the next one take 20? Who knows, but I think now at least I understand why they're doing it.

Tuesday, July 22, 2014

Top 7 Android Tools

I just finished up my second Android app and this time round found a lot of really useful tools to help me through it, so I thought I'd make a little list of my favorites.

1. Android Studio (download)

This one's easy. I initially tried to use Eclipse for creating  Android apps, but it seemed to complicated and slow and it never really came together. As soon as I heard about the release of Android Studio I checked it out (even though it was really early and buggy) and found it to be great. I think a lot of what I like about it has to do with the back-end, Intellij is a really nice looking IDE, it's strange that I didn't even know it existed before Android Studio.



The exciting news on this one is they've just gone into Beta (finally). They're at version 0.8, and with it they've made it a whole lot more intuitive, and hopefully there'll be less updates that break existing features. 

Before they went to Beta, I stopped updating at version 0.2.9, so I was intrigued to see what had changed. First of all, the whole thing looks like it's had a bit of spit and polish, everything looks more cohesive. The new project wizard looks really good too, with more templates added. You can create your app for phone, Android wear and even Glass all in this tool now too. Here's one of my favorite new screens, it's not a big deal, but they've just done a nice job


2. WiFi ADB (download)

I came across this one pretty far into development, but it worked nicely into my process. In case you're wondering, my process is the following. Find some time to program, program anywhere there is space. The downside to this style is sometimes you'll be lacking a USB cable, and the cable you need is in the room where your child is currently sleeping. Now you might say that I should just buy another cable, well I did, but I like this even better.

Somebody has created this little utility for free (FREE!) that lets you remotely debug your app. The downside is you have to have a rooted device for it to work, but I'm assuming if you're developing an app for your phone, you've probably already done this. It's literally a 2-step process. Open the app and select ON. Now go to your computer and type in what it says on screen and you're connected.

Admittedly I had a couple of times where it went a bit wacky, but all I had to do was reset the app and it was good as gold.

3. CatLog (download)


Obviously, this has something to do with LogCat. Well, since I was creating a productivity app I was trying to test it out during my most productive hours, at work. Unfortunately, I can't plug my phone in and spend half an hour looking through crash logs at work when something fails in my app. That's where CatLog comes in. Every time the app would crash (and it was often enough to require this, trust me) I could quickly open this app and filter it down to find our roughly what had happened. If it was clear, I could just take a note down and then know what I was trying to fix later.

This became really important later when I had a bug that only crashed the app very sporadically, a real life saver.

4. Android Debug Bridge, ADB (information)

Obviously ADB isn't so much a tool as the very foundation of communications between your phone and the IDE. It's basically a command line especially designed for programming. Anyway, I actually haven't used a bunch of the features, but I know it's got a lot of them, and if you're ever stuck, take a look into it, ADB can probably do it.

One thing I had to do recently was get access to the saved file of my app. Now because I hadn't planned on this being a feature, I stored it in the internal memory, which apparently is pretty locked down. Luckily, again, I have a rooted phone, so using ADB and SU (super user) I was able to locate the file with the following commands. First, plug in your phone and open a command prompt in the same folder as the ADB. If you installed Android Studio, it's going to be Android Studio\SDK\Platform-Tools\. Now type in "adb shell" followed by "su". Your phone will ask you to give it super user access. At this point you can navigate down to the file you need using ls (list directory files) and cd (change directory).

Here's where it gets a little more complicated. Now if you need to copy that file to your computer from your phone, you'll have to locate the folder it's in and type in "CHMOD 777 {your filename}". This gives you full read/write/execute permissions on the file. Now open another command prompt and adb shell again. This time use the command "adb pull {file location on phone} {file destination on computer}. That should copy it.

Again, all of the stuff ADB can do can be a little complicated, but it's so powerful.

5. GrepCode (information)

GrepCode is just an open source reference for all android code. If you're thinking about extending or copying some base functionality that you just want to tweak a little, this is the place to start.

6. Libraries For Developers (download)

There are a lot of really nicely made open source projects on GitHub with people making custom libraries for use by anyone. Obviously, you can search through GitHub yourself to find these controls, OR you can download this app, and just casually browse through probably about 50 of them. They cover a lot of stuff, and it's always being updated. If you have any need for transitions, animations, custom widgets, popups, graphics, etc. this is the place to go.

Even better than this app, are the many projects it points you to. Most of which can be included in your app with only a simple gradle include in your build.gradle file (I'm assuming you're using Android Studio at this point).

7. Android Developer References (link)

This one's a given really, but I think it needs to be pointed out that Google has made a really nice website, with advice on just about every aspect of making an app. Not only do they have the API, available online and as a reference in the SDK, but they also have longer real world examples of the more common stuff. I found myself using this just about every single day.

Saturday, July 19, 2014

To Learn A Language (Part 1)

The first language I learned was UniComal. I'm not sure why the school decided that in 1997 this was the best choice to introduce a generation of kids to the wonders of programming. I'm going to assume it was some kind of budget issue combined with the fact that all the computing teachers were over 50. I don't remember writing any code, but apparently it was one of these language -
10 PAGE
20 FOR number:= 1 TO 10 DO
30  PRINT "HELLO, WORLD!"
40 ENDFOR
50 END " "
And if my memories are correct, the kind of things we ended up creating looked kind of like this??


So not really setting the world alight.

Next step, once we elected to continue on with computing for to start learning Visual Basic. I'm not exactly sure what version we were working on, I'm pretty sure it wasn't VB6, but who knows. This was my first taste of real programming and I remember enjoying it, although again, we weren't really learning anything useful. I think this is what the final project was -

 

It's possible the whole Scottish computer science secondary school curriculum runs on being able to code Hangman. Anyway, I continued on to my final year in computing, until it became all about requirements specifications and no actual coding. I figured this is what programming in college would be like (I was very wrong), so I stopped programming altogether.

I dabbled over the years in VB6 and some C++ (also some PLC work, but I don't think that truly counts), but I didn't restart programming in full again until I was about 25 and picked up Java.

Java was unlike all the other programming languages I'd used so far. Looking back at VB6 now, I see it's probably not all that different, but when I started learning Java, it felt like a different world, one full of curly brackets and semicolons. That was before I even discovered Object Oriented Programming. Luckily I had a good book to see me through. I tried a few of them, but the one that really stuck was Heads First Java


If you are interested in Java, I really recommend it, they break everything down into little sections and show you multiple ways of looking at the same problem. Plus there are little projects at the end of each chapter to make things stick.

While I understood Java pretty well I was a little worried that I might be missing some style things, seeing as I'd only learned from one book, so I decided to follow it up with a free online programming course. Stanford's Programming Methodology (introduction to computer science). It was awesome, I felt like I was learning a little bit of new stuff while also recognizing a lot of general principles, it really helped reinforce everything, plus the Professor, Mehran Sahami was great (sadly I'd say he was better than all of my Professors in College).

When I started learning Java it was always in the goal of being able to write my own app. I'd learned a little about Android, and always thought it'd be fun to write something. I had no idea what, or how long it would take, but I knew I couldn't do it without Java.

Anyway, this has already become much longer than I'd originally planned, so I'll stop here and resume another day. If I have any readers yet, I hope you found this interesting.

Thursday, July 17, 2014

Swapping XML For YAML

I'm starting to write a simple utility program at work to make my life easier. As with all of these kind of things, I found myself needing somewhere to store my data to file, and I automatically thought of XML (I'm sure I'm not the only one).

Halfway through setting up serialization, I remembered a blog post I read a while back about alternatives to XML. Jeff Atwood wrote a piece about the Angle Bracket tax. This section in particular seems to jump out at me
1. Should XML be the default choice?
2. Is XML the simplest possible thing that can work for your intended use?
3. Do you know what the XML alternatives are?
4. Wouldn't it be nice to have easily readable, understandable data and configuration files, without all those sharp, pointy angle brackets jabbing you directly in your ever-lovin' eyeballs?
I realized I was defaulting to it. It wasn't the simplest thing for my use (at some point somebody may want to edit it by hand). I didn't know what the alternatives were, and it would be nice to be able to read it easily.

I read the whole post, so I guess by that point I did know what the alternatives were, but I decided to give YAML a shot. YAML stands for YAML Ain't Markup Language. It's like a more human-readable XML.

After a little research I came across YAMLSerializer for .NET, which did exactly what it said it would (thanks Nuget). With the following code -
        YamlSerializer serializer = new YamlSerializer();
        serializer.SerializeToFile("filename.yaml", obj);

I got exactly what I wanted -
%YAML 1.2
---
!Patcher.Patches
PatchList:
  ICollection.Items:
    - DateCreated: 2014-07-16 01:50:53.542Z
      Filename: Lighting v2.2.1.35.dll
      IgnorePatch: False
      FilenameFull: C:\Users\Chris\Google Drive\Programming\Microsoft\Patch Information\Patches\Lighting v2.2.1.35.dll
      IsFolder: False
      Revision: 2.2.1.35
      Destination: null
      Extension: "2"
    - DateCreated: 2014-07-16 01:51:10.300Z
      Filename: Lighting.dll
      IgnorePatch: False
      FilenameFull: C:\Users\Chris\Google Drive\Programming\Microsoft\Patch Information\Patches\Lighting.dll
      IsFolder: False
      Revision: 2.2.1.38
      Destination: C:\Bin\
      Extension: dll
...
Beautiful.

What's that? You want to load it back in? Here it is -
        YamlSerializer serializer = new YamlSerializer();
        obj = serializer.DeserializeFromFile("filename.yaml")[0];

Count me as a fan.

Tuesday, July 15, 2014

Recording Android Apps With Sound!

I wrote my first android app about 6 months ago now. It was a pretty simple affair. My daughter is about 2 years old, and she was just starting to learn her colors. What better way, I thought, to teach her them, than to write a little app that shows and says the colors. Color Bubbles was born, (here for Apple).



Pretty simple stuff, but my kid loved it. It took almost no time to get a prototype together, it was about as simple as apps get, but it still took me a few months to get it all wrapped up. I already knew Java, so getting all that working was simple, but then I decided to try and make it multi-platform. I managed, but I spent a fair while (and an extra $99) to make it happen (Spoiler Alert: I haven't recouped costs yet!).

Anyway, I finally managed to get it released on both platforms, but my final hurdle was getting a video of the app in action, with sound. You'd think it would be easy, but no, apparently it's not. If you're interested in increasing the budget of your app even more, there are a bunch of places willing to take your money to create a video and let the world know about your app. I won't judge if you do it. This woman does look like she knows what she's doing...

Like I said, cheap, I mean, it's only a starting price of $279!

Well I created a simple video for the app with no sound, but without the sound it looks like somebody's running a test to make sure the monitor colors work right. I heard a rumor about CyanogenMod adding screencasting capabilities, so I spent a night rooting, then flashing, then flashing again, then flashing one last time (maybe this is the one!). Finally it worked. I was so happy, two hours of messing around and finally I can record the sound on my app. But wait, no, the sound doesn't work. Apparently nobody thought mentioning that was worthwhile!

Anyway, not to bore you with my long list of nights wasted (it's very long), but I finally came up with a solution, and it's free. Presenting you with Genymotion and CamStudio.

Genymotion is an alternative to the android emulator that actually works. I'd never used it before Monday, but I tried it out because a friend mentioned it not working (he was wrong). I've always just debugged on my phone and tablet, and left it there, but now I can see this would be a great alternative. It's smooth and it starts up really quick (2-0 to android emulator). It's so fast in fact, that it even runs my app at full speed with sound. Genymotion is free for personal use, but ramps up quickly in price if you want to use it in a professional way. Unfortunately recording the screen is not free, but that's where CamStudio comes in.

CamStudio is a little open source project that lets you record the screen of your computer to file with very little hassle. There's a couple of codec options and it lets you record only a section of the screen if you want. One weird quirk was that the website for CamStudio (not the sourceforge link above) seems to have a dodgy download that contains a trojan. Obviously, don't do that, and follow the link I gave and you'll be fine (you're already using a good antivirus that would have caught that anyway, right?).

So, I used both of these applications together and voila, here are the results.


If you're interested in the app, please download it, it's only $0.99 and even though my kid is now 6 months older than when she first saw it, she still loves it. It goes without saying that she knows her colors now as well, but I don't know if I can take all the credit for that!

Thursday, July 10, 2014

Stuck In The Basement With Serial

Cloud computing seems to be the in thing right now. It's cool, it's always available, it's designed for web and mobile. In short, it's awesome! I'd love to get in on that action. I'm always trying to come up with some idea that could become something, I'm sure all developers do, we're all looking for the next big thing.

Someday I'd like to work for a software company that creates this kind of software. I'm not sure if this is a pipe dream, there's a lot of people out there with real CS degrees coming out of college every year. To try and accomplish this goal, I've tried to make sure I'm covering as much ground by myself as possible. I've worked on Java, VB, C#, Objective C and WPF. I've just started working through the Stanford Programming Abstractions course. Looks good so far, with the only caveat being a Visual Studio 2005 requirement (thank you MSDN partners network).

Here's the thing, even with all this self learning going on, you know somebody's going to ask what I did at work. Well, here it is. I work on VB.NET exclusively, with all GUI's (except one) done in WinForms(?!!?). Yes, that's right, we're here in 2014, and most of my work is being done in WinForms.

This brings me nicely to the topic of this post. Nearly all the software I write is to communicate with hardware. Here's some things that suck about that -

1 - Nearly all this hardware uses serial communication. Christ, once I actually had to write a CANBUS interface. There are the occasional TCP connections, but generally we don't even use that because they're "unreliable".

2 - The people selling this hardware are not making money because of their software skills. Generally they create a nice piece of hardware, and then ten minutes before it ships they create a crappy serial interface. Usually they focus all their effort onto their internal software, not on making my life easier.

3 - The serial interface now exists. You might hope there would be some kind of structure to the commands. Say turning on the light might be called TurnOnLight. Nope it's called FHO:4C. When you send it, you might also be hoping for some kind of reliable response. Again, wrong. One response might get you a 1, the next gives you nothing. Now try and write some structure into that nightmare.

4 - Nobody is ever going to be impressed that you could write 3 serial interfaces.

5 - You'd be surprised how many different hardware options there are, and how many of those serial commands you just figured out for this one product actually don't exist on this other product. Oh great, software switches to turn on sections!

5 - Did I mention that it's all in serial.

Anyway, that's how I feel about serial. Someday, I'd love to get away from hardware. That's why I spend so much time working on my own stuff. I know most of my ideas probably already exist, but doesn't it help me to show I'm trying?

Monday, July 7, 2014

Parenting.NET

while (child.age < TODDLER) 
{
  if (child.awake)
  {
    if (!this.awake)
    {
      wakeUp(DateTime.Now);
    }
    
    switch (child.state)
    {
      case State.Awake:
        playWithLegos(child);
      
      case State.Tired:
        readBook(child);
      
      case State.Bored:
        if (reallySunnyOutside)
        {
          applySunscreen(child);
        }
        goToPark(child);
      
      case State.Tantrum:
        hideInBedroomWithOtherChild(TANTRUM_DURATION);
      
      case State.Hungry:
        if (checkFridgeIsNotEmpty())
        {
          makeFood(child);
          makeFood(this);
        }
        else
        {
          makeFood(child);
        }
      
      case State.Funny:
        tellJokes(this);
        makeShadowPuppets(this);
        
      case State.ReadyForBed:
        if (child.daysSinceBath >= ACCEPTED_SOCIETAL_BATHING_REGIME)
        {
          giveBath(child);
        }
        goToSleep(child);  // TODO - multi-thread this (can take a long time)
    }
  }
  else
  {
    if (houseIsNotPerfect)    // Possibly refactor this to houseIsAcceptable?
    {
      cleanHouseAndGarden(this);
    }
    else
    {
      sleep(1000);
    }
  }
}

Friday, July 4, 2014

I thought I'd tested that!

I had this big plan today to write about prime numbers, but after some initial investigation it turns out it's way more complicated than I thought. Now don't panic number lovers, I'm still going to cover it, I just want to make sure I do the idea justice.


So anyway, back to today's post. On Tuesday night I finally released my 2nd app, +task. Its a little to do list organizer I wrote for work.

I've been working on it for about 6 months now. This may seem like a lot of time, but any side project I work on only gets about 5 hours a week AND I just had a new kid (he's nice, so I want to hang around with him).


You might also be wondering why I don't just use any of the other numerous task apps out there. I tried. I used Keep, Wunderlist and many more I can't name. Turns out they didn't work perfectly for me, so being a nerd I thought I'd write my own.


I'd be remiss at this point if I didn't at least recommend it. I like it, give it a shot. It's available on Android only and is completely free. If you like it please rate it.


OK, so let's finally get down to the business of this post. It turns out I'm bad at testing, I always knew I was a little slap dash. I'd like to think over time I've become better, especially at work, but apparently when it comes to my side work, my testing is woefully lacking.


Case in point. I'm already on release 1.2. That's right, two updates in 3 days. I thought I'd done a good job, but on day 1 I found a bug and on day 2 a friend showed me another bug. These weren't little things, both crashed the app, and obviously left me with a feeling of shame.


It turns out adding features to your app in the week before release without proper testing is ill-advised. I actually had to remove a feature as the bug was so deep it was easier to remove than fix. Maybe someday it'll be back, who knows


I think the solution to this needs to be some kind of testing regimen. I don't have unit tests (I'm not sure if my app fits for this idea, maybe I'll do a post about this in the future).


Seeing as I can't be the first person to have had this epiphany, I did what I always do when I know nothing about a topic - to google! Then, obviously, follow up the initial info with a trip to stack overflow.

It turns our there are two major frameworks available for functional tests. Google has one called the Android Test Framework. Looks like it's completely integrated, but the major downside I see here is it's limitation to android 4.1 and above. That's a fast no for most developers. The main alternative looks to be called Robotium. From first glance it seems like it has everything Android Test Framework has and more, plus an easier command structure. It's hard to say how well either are for first timers (I think I'll try them out in the future and get back to you), but it's definitely a place to start. If I can offer you nothing else, here's a Stack Overflow question about which one's best.

I think my final advice is test. Somehow. If you're already testing thoroughly then nice job, you're clearly a better programmer than I am.

Tuesday, July 1, 2014

Introduction

Hi. Let me introduce myself. My name is Chris Johnson. That's right, Chris (11th most common) Johnson (2nd most common). Turns out there's actually 11,377 of us in America, 13,799 if we include the Chris and Christopher (and I will). At least I'm not lonely. In fact, if you're male (I assume people named Chris are) and live in the USA, there's approximately a 1 in 11,000 chance you share my name. So, kudos.



(I've never been great at introductions so just stick with me on this blog post, it can't get worse)

I grew up in Scotland and now live in Minnesota with my wife and 2 kids. I went to school for Physics and Math but realized a little too late my lack of interest for it. I now work as en Electrical/Software engineer for a little company in the Twin Cities. 

This blog is going to be about programming. I know, you're probably thinking, "Why would I read this when there are millions of other blogs out there?"

Fair point, what with all the Jeff Atwoods and Nick Bradburys out there, why bother? Well, hopefully I can provide a different point of view for you. 

I didn't go to college for computer science. I'm not some crazy computer genius who didn't go to college and has been coding since diapers (Jamie Zawinski, specifically). I'm just a regular guy who likes to program and wants to learn more about it. I thought I could use this space to talk about some of the things I work on and learn about and share some of that. 

I find the easiest way to code is to jump right in. Of course the problem with this is that things are never quite as pretty as I might like them to be. Thus, the name of this blog - Eternally Refactoring - describes most of my work.

For background, at work I'm using VB.NET with a little WPF and C# thrown in for good luck. At home I've been mostly working on Android and iOS apps. I'm hoping to keep learning and cover more as I go. 

If any of this sounds interesting, stick around. I'd be glad to have you. 

Chris.