Sunday, February 27, 2011

Reversing integers

I think I found a better way of reversing integers with a little Google help. I'll have to make sure I understand it on paper before putting it to code.

Basically it's this:

A number 12345 % 10 is 5. If you stay in int-land then 12345 / 10 is 1234. So, keep asking for the number % 10 and then divide by 10 and repeat until your number is 0. That solves the problem of getting the numbers in reverse order, but how do I store that?

Hm. Well, it's something. Further investigation is necessary.

EDIT: I suppose I could keep adding the numbers from the mod statement to a variable and just multiply them by 1, 10, 100, etc.. (10 ^ loop iteration).

(1*5) + (10*4) + (100*3) + (1000*2) + (10000*1)

Saturday, February 26, 2011

PE#4 non-portable

The itoa() and strrev() functions are not available on the netbook. A few resources say that they are part of some but not all compilers. Huh.

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

Python

In the mornings I drink coffee and try to wake up enough to put my pants on before my shoes. It has a 50/50 success rate.

While my cortical fortitude is returning I read about a dozen online comic strips, several gadget blogs, several news sites, and then I settle into Reddit.

Since I've started paying attention to programming-related stuff I've found that the most commonly mentioned language is Python. Most of the interesting things I'm wanting to do (GTK, OpenCV, etc) have Python bindings, and it seems to be the language of choice- perhaps simply because it's not c.

I figure after I get a fair bit into C I'll try to dabble in Python. Maybe late summer? I feel like I'm still just scratching the C surface.

Friday, February 25, 2011

Project Euler 4



I know this is probably an embarrassing brute force of the problem, but I'm happy that I've used a loop in a loop successfully and that it also got the right answer. It takes 8.13s to run (on my PC).

Integer palindrome detector



This was pretty easy...but I feel like maybe there is a more graceful way of reversing an integer without getting strings involved. All said and done I'm glad to get into some string manipulation functions.

I did this on my PC, my other code for EP#4 is on my netbook. Maybe before bed I'll copy/paste this function into my netbook's code and finish this problem off.

Thursday, February 24, 2011

Maybe I got it?

A prime number has only two multiples, 1 and itself.

A composite number has more than two multiples factors.

A composite number is guaranteed to have a multiple factor that exists somewhere between 1 and the square root of that number.

The square root is the tipping point on the multiple factor balance beam. I'll try to write out some examples.

Lets take 16's multiples factors:

1*16
2*8
4*4
8*2
16*1

Once you get past the square root the numbers "flip around" such that you're re-showing that 2 and 8 are multiples factors.

Let's try 100.

1*100
2*50
4*25
10*10
25*4
50*2
100*1

Again we see that IF another multiple factor past the square root exists, it will have been detected prior to the square root.

I hope that this is the reason the square root trick works or I'm going to have to take it on faith and that just irritates me because it breaks my "don't use it unless you understand it" rule.

Project Euler 4 WIP

Number Four:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

Matt gave me some advice about the prime finder that I'm hoping to dig into in March. Work might be hairy for the next few weeks.

Step 1 of problem 4 is making a palindrome detector. I wonder if I can cheat a bit... I know that there are some functions in one of the usual libraries for reversing strings. Can I cast a number to a string and then reverse it? I doubt I'll be able to get any code going until late next week at best. Too tired to think right now.

EDIT: Wikipedia touches on this.
http://en.wikipedia.org/wiki/Primality_tests#Na.C3.AFve_methods

That shouldn't have worked

Now I'm convinced I got lucky and maybe I'm mis-remembering the square root thing.

Take 16 for example... you can't look for all the primes from 1-16 by looking only up to 4!

I'm going to copy the program over to my Win7 machine and run it without the square root trick. My netbook gets super toasty after chugging away for more than a few minutes.

Wednesday, February 23, 2011

Project Euler 3



http://projecteuler.net/index.php?section=problems&id=3

I solved this one, but I'm not happy about it because I somewhat cheated. I remember from somewhere that when testing for primes (or I suppose any multiple?) you only need to test from 1 to the square root of the number in question. I was able to implement this and the program ran a bit over two minutes before finding the answer. Before I was testing all the way up to the number and the program was taking forever and I got impatient.

Anyways, I call it cheating because I don't understand why it works out like that. Why am I allowed to only test up to the square root of the number in question? Further study is needed.

A few more things about this program I'm not happy about:

1) I'm using "unsigned long int" because I didn't want to worry about my test number being to large for int or long int. I am being lazy (hard to do things fast on the netbook) and I don't want to find the smallest type that will fit my number so I'm just going whole hog.

2) I crammed some efficiencies into my prime finder function that I want to do more gracefully. The first is that if the number being tested is even it bails out, and the second is that in every loop it tests for how many divisors have come up without a remainder and bails out if it's more than two. I'll post the source after I clean it up.

3) The compiler gives me two warnings that I ignored (and hoped for the best).

What I AM happy about (other than getting the right answer) is that I am using two source files - one containing what I've pastebinned above, and another that just has my prime finding function that I wrote about in late January (plus a few optimisations). This is the first time I've written a program that uses two source files.

So, tomorrow I'll try to get a solid answer on why the square root trick works and then I'll start on problem 4.

Tuesday, February 22, 2011

MIDI link

I came across a project on Hackaday that had a component that sent MIDI info from a piece of software to a MIDI channel in a DAW. This is one of the missing pieces of knowledge I need to get a grip on before doing one of my projects.

http://nerds.de/en/loopbe1.html

The website is down right now, but hopefully it will come back up soon.

Project Euler 3 WIP

The third problem is:

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143?


Should be simple in a brute force way - first find a factor, then test if that factor is prime. If it is, store it. I shouldn't have to keep checking if it's bigger than the last stored factor since each found will be larger than the next. I can re-use the prime finder program I made a while back - although I have to do some obvious improvements to it first.

I'm hoping that this problem won't give an instant result like the others have. If there is a noticeable delay between execution and result then it will be easier to know if any improvements I make are making the program swifter.

I can't think of the word that means "to make something more efficient".

Monday, February 21, 2011

I feel dumb

I thought I was all cool for solving these (well, newbie cool). Looking at the forums for the problems I realize how heavy handed my approach is! Maybe some day I'll circle back and make my code more efficient.

One example with the Fibonacci problem is that it seems every third term (and only every third term) in the sequence is even. My "are you even?" check wasn't necessary if I had just ran the sequence through a few more terms per loop and added the result.

I'm not sure I'd have EVER noticed that!

There were more complicated ways to make the code faster and more graceful, but they're a bit over my head right now.

Project Euler 2



Problem 2: http://projecteuler.net/index.php?section=problems&id=2

I took a few typical shortcuts and I used a while loop just because I've been using them in Matlab lately and I wanted to do it in C. I can't shake the dirty feeling for using "break;" like that, but it's legit since it works.

Two down in one evening. Not bad, but they look to start getting hard about now.

Project Euler 1



I'd like to get through as many of the Project Euler problems as I can in the evenings. I won't have a lot of time to dig through the next chapter of the GTK+ book until mid-March I figure and these problems can usually be knocked out fast once you figure out what has to be done.

Problem 1: http://projecteuler.net/index.php?section=problems&id=1

As long as you know how to test for remainders in a division you're golden. No real trick here.

Nothing new

The impending conference we're hosting at work has taken up all my energy. I have time in the evenings but no motivation!

I've been working with Matlab a bit so at least I'm doing something programming related. Nothing too difficult - mostly just cycling through data returned from the system for things of interest and making note of it, or displaying data. Matlab gets slow as molasses when you're dealing with displaying graphs made of thousands of points.

Speaking of graphs, I've been struggling to find an easy way of graphing data using GTK. I know that GTK is just a framework and you need other things (Cairo, etc) to do displays but I thought I'd find something quick and easy to use for plotting line graphs. Maybe I'm not searching with the right terms.

Tuesday, February 15, 2011

not programming

Gift cards at birthdays and Christmas have provided me with a sizable iTunes video collection. Some Mad Men here, Doctor Who there, It's Always Sunny, and so on. I spent an evening trying to figure out how to convert that video to .avi or somesuch so that I could stream it over my XBox360 but I failed and wasted a good deal of time. There are too many scammy programs that come up in searches that I don't trust entirely - maybe being braver would have yielded results but I don't have the time to recover if something goes wrong.

So I figure I'd just spend the $99+shipping for an Apple TV. It's just a little box with a power and HDMI connection that lets me do four things:

1) Rent media and stream it (no local storage)
2) Stream media from my iPhone
3) Stream media from iTunes on my PC
4) Watch Netflix streaming

I got 1, 2, and 4 working out of the box, but number 3 was a beating to get working. The Apple TV detects my computer (and vice versa) and it will try to stream stuff but it takes forever. A three minute music video took 15 minutes to start playing because the ATV waits for a buffer to build.

I'm mixing my tenses here, describing solved problems is tough.

Anyways, after much reconfiguring and rebooting I discovered that running a wired network connection to my PC fixed everything. This narrowed it down to my router or my wifi card in the PC.

I slept on it and figured I should do a test of what my upstream is doing so I tried to FTP a big file from my PC and found that my upstream was borked. It kept bouncing between 0 and 40 kbps rapidly. I did a search for my wifi adaptor and Win7 and found that it's a known issue. One driver update later and I've got streaming happening.

It's been a long time since I've had to do that kind of hardware troubleshooting at home. Everything has been working so well the past year since I built the new machine.

Not programming, but it prevented me from programming so it's peripherally relevant. So far the ATV has been worth picking up because now I can watch stuff on my TV instead of hunched over at my PC, and the Netflix streaming is far more reliable than the XBox simply by virtue of not constantly trying to adjust the quality which causes it to skip 1 second or so forward.

some proof

http://library.gnome.org/devel/gtk/stable/GtkContainer.html#gtk-container-get-children

There is a function for returning a list of all the children in a container which pretty much shows that each container widget keeps track of its respective child widgets. This must happen once you pack a widget into a container, and it explains why I can lose the original pointer to the widget after the packing and still have the signal callbacks function.

Sunday, February 13, 2011

GTK Chapter 3 Example 1



This does what it looks like - makes a window with four buttons arranged vertically.

I don't have time to go into this in detail, but a big question is the behavior of the boxes. When you click them they vanish and the other buttons automatically adjust to fill in the missing space. It's not the movement that bugs me, it's how the callback to catch the button press signal works. I'm recycling my variable "button" for each new button, so how does that callback know which button is which? My best guess is that there is something going on under the hood that keeps track of things, but I don't really like that answer. If I was doing this without the book's help I'd have made an array of pointers to GtkWidgets and incremented the array with the loop. Is that not necessary?

Friday, February 11, 2011

GTK+ 3.0

I was worried that I'd spend months learning GTK and then get blindsided by a new version that would require a whole lot of work to re-learn. Looks like I picked a good time to start learning because the next major release just came out. Nothing looks crazy that would break any of my current knowledge, and it's going to be nice getting in early on it since it's going to be a long time before the next major release that might set me back.

This weekend isn't looking good to get any practice in. I set up a skeleton GTK program with the basics so that I can easily plug in new stuff I learn. The next few things are going to be container widgets (horizontal containers, vertical containers, containers with a movable divider, tables, etc) so my template program has a GtkWindow and the callback function and signal handling to close the program. All I have to do is drop in the new stuff and connect it to the existing GtkWindow. It really saves a lot of time.

I have a strong urge to go back to the other project I was working on that involved reading files from work. There is a new format looming on the horizon and I want to have a strong grasp of the current one before learning about the next one.

Tuesday, February 8, 2011

Exercise 2-2 in the GTK book



I'm getting pretty good at schlepping through the API docs to figure out what all these properties are called.

2.2 was about catching signals that happen when a widget property is modified (notify::title) and using g_object_set instead of the canned functions (which I commented out to show what I was reducing them to). I'm not sure yet which I prefer. The book says to use the canned functions when available and the purpose of using g_object_set in this exercise is to prepare for the fact that not every property has a corresponding function to change it.

So that's chapter 2. Chapter 3 starts with more elaborate container widgets for holding more than one thing. Onwards and upwards?
I've been a bit light on the journaling side of things, but I am programming. Last night I started getting into the 3rd chapter of the GTK book. I have yet to finish the second exercise of chapter 2, but that's because rewriting a program is kind of dull. I certainly DO need to finish that exercise for the sake of completeness, but not right now. I think the point of that exercise is to force the reader into the API which is just a timesink and I'm really relying on google to get me to the right spot.

I'm impressed with how GTK handles tables.

My goal this week is to do the first few Euler project tasks and work on my prime finder. Step one in making the prime finder not so naive is to eliminate searching for factors in even numbers.

Saturday, February 5, 2011

I worked some on the second exercise problem in the second chapter of the GTK book. It asks to rewrite the first exercise using only property get/set functions as defined by GObject instead of canned functions for doing the same thing. The other part is learning how to use "notify" events. "notify" lets you monitor the state of things and connect callback functions to state changes. So in the example you're changing the title of the window but you've also got something monitoring the window title that happens if the title changes.

Bleh, can't write coherently after midnight. Waiting on Ana to get home.

Thursday, February 3, 2011

ice

I've been working from home the past few days so I haven't been super motivated to stay on my computer and do some programming.