I'd like to get this code to compile:
http://zetcode.com/tutorials/cairographicstutorial/basicdrawing/
I have a fun idea for a program but I can't do it unless I get a handle on embedding a cairo-based drawing in a GTK window.
http://developer.gnome.org/gdk/stable/gdk-Cairo-Interaction.html
I'm 28 (oops, 29 now) and I want to be C literate by the time I'm 30. That's two (one) years to become competent at something I've been wanting to do nearly my whole life. No pressure.
Friday, September 30, 2011
Thursday, September 29, 2011
quick detour
I took a brief lull in activity at work to knock out another important piece of code in my case analysis program (not so much analysis right now - mostly just parsing).
Python lacks a #define statement so I can't alias words to numbers. I had really hoped to do this to pair column names (human readable) to numbers (parser readable).
Instead of:
#define OPENDATE 0
#define CLOSEDATE 1
#define NOTES 2
I am faced with just making them proper variables:
OPENDATE = 0
CLOSEDATE = 1
NOTES = 2
and so on.
I stumbled across a different solution I like in example 3.20: http://diveintopython.org/native_data_types/declaring_variables.html#odbchelper.multiassign
I'll flip a coin to decide which is more readable and then put it in. The code for returning a specific column is already in place. Took me longer than it should have - I've forgotten too much about Python in the past few months.
Python lacks a #define statement so I can't alias words to numbers. I had really hoped to do this to pair column names (human readable) to numbers (parser readable).
Instead of:
#define OPENDATE 0
#define CLOSEDATE 1
#define NOTES 2
I am faced with just making them proper variables:
OPENDATE = 0
CLOSEDATE = 1
NOTES = 2
and so on.
I stumbled across a different solution I like in example 3.20: http://diveintopython.org/native_data_types/declaring_variables.html#odbchelper.multiassign
I'll flip a coin to decide which is more readable and then put it in. The code for returning a specific column is already in place. Took me longer than it should have - I've forgotten too much about Python in the past few months.
More divisor function thoughts
I had some time to sit down with a pen and paper to figure out a better way of getting all the divisors out of a number.
I don't think there are any clever tricks using recursion I can use. I went down that path because it worked for something else but I don't think it will work here.
This makes sense in hindsight (and it's a testament to how poor at basic arithmetic I am) that I only need to test to half the number.
For example, the number 90 has 10 divisors: 1, 2, 3, 5, 9, 10, 18, 30, 45, 90. Obviously I don't need to test beyond half the number (which holds true for odd numbers, too).
I might have even stumbled upon a way of detecting square numbers.
64's divisors are 1, 2, 8, 16, 32, 64. If you see the symmetry in both 90 and 64's divisors you can see a pattern.
90: 1,2,3,5,9,10,18,30,45,90
Taking the outer numbers and working out way pairwise inward we have:
(1,90)(2,45)(3,30)(5,18)(9,10)
These are factors of 90.
64:1,2,4,8,16,32,64
Doing the same thing we find a slight difference - there are an odd number of divisors.
(1,64)(2,32)(4,16)(8,?)
We of course know that 8*8 is 64. This makes me wonder if all squares have an odd number of divisors, or if there are number with an odd number of divisors that
aren't squares.
25 is a square and has an odd number: 1,5,25
So, just some thoughts. I could quickly spin off my program to test for stuff like that.
Anyways I can quickly change my program to only loop n mod x and test for 0 such that x only goes to half of n instead of all the way up to n.
I don't think there are any clever tricks using recursion I can use. I went down that path because it worked for something else but I don't think it will work here.
This makes sense in hindsight (and it's a testament to how poor at basic arithmetic I am) that I only need to test to half the number.
For example, the number 90 has 10 divisors: 1, 2, 3, 5, 9, 10, 18, 30, 45, 90. Obviously I don't need to test beyond half the number (which holds true for odd numbers, too).
I might have even stumbled upon a way of detecting square numbers.
64's divisors are 1, 2, 8, 16, 32, 64. If you see the symmetry in both 90 and 64's divisors you can see a pattern.
90: 1,2,3,5,9,10,18,30,45,90
Taking the outer numbers and working out way pairwise inward we have:
(1,90)(2,45)(3,30)(5,18)(9,10)
These are factors of 90.
64:1,2,4,8,16,32,64
Doing the same thing we find a slight difference - there are an odd number of divisors.
(1,64)(2,32)(4,16)(8,?)
We of course know that 8*8 is 64. This makes me wonder if all squares have an odd number of divisors, or if there are number with an odd number of divisors that
aren't squares.
25 is a square and has an odd number: 1,5,25
So, just some thoughts. I could quickly spin off my program to test for stuff like that.
Anyways I can quickly change my program to only loop n mod x and test for 0 such that x only goes to half of n instead of all the way up to n.
Tuesday, September 27, 2011
New GTK+
http://www.webupd8.org/2011/09/gtk-32-released-with-html5-allows.html
You know... I'm not really taking advantage of all that VirtualBox allows me to do. I'm still running Ubuntu 10.10 because I'm scared of the big UI refit that 11+ added. There's no reason for me not to just make another VM and test the waters.
You know... I'm not really taking advantage of all that VirtualBox allows me to do. I'm still running Ubuntu 10.10 because I'm scared of the big UI refit that 11+ added. There's no reason for me not to just make another VM and test the waters.
Win32
One of my (ex) coworkers had to get up to speed on Windows programming and learned enough of the Win32 API (not MFC, or not much of MFC if I recall) to get the job done.
I spent last night perusing some Win32 tutorials and it doesn't look that hard. It has its own special weird things you have to do "just because", and the event handling is called message handling but other than that it looks rather analogous such that it wouldn't be too tough to get familiar in a hurry if I had a free weekend.
The way it handles messages is (at first glance) organized different than how GTK does it. In GTK you explicitly list all the signals you want to catch and pair it with a callback function. In Win32 you have one callback function paired to a window and in that callback function you use a big switch statement to handle any incoming messages.
The reason I mention this is that I'm still really fuzzy on all these Windows programming options. I know that .net is a framework and MFC is sort of the C++ option for Win32 (right?) but I have no idea what WPF or COM or any of those other TLA mean (beyond a quick Google search anyways).
As an amateur level hobbyist programmer that wants to evolve beyond simple ~100 line console programs to useful GUI-based programs that do interesting things I feel very nervous about hitching my wagon to one particular OS-specific framework. I picked GTK because it has decent cross-platform support (excluding OS X unfortunately) but it concerns me I almost never see programs for Windows written in GTK.
I'm just being paranoid about spending time learning something that isn't useful in a lot of situations. That being said I'm happy that the time spent with GTK at least allowed me to understand the Win32 API on a superficial level.
I spent last night perusing some Win32 tutorials and it doesn't look that hard. It has its own special weird things you have to do "just because", and the event handling is called message handling but other than that it looks rather analogous such that it wouldn't be too tough to get familiar in a hurry if I had a free weekend.
The way it handles messages is (at first glance) organized different than how GTK does it. In GTK you explicitly list all the signals you want to catch and pair it with a callback function. In Win32 you have one callback function paired to a window and in that callback function you use a big switch statement to handle any incoming messages.
The reason I mention this is that I'm still really fuzzy on all these Windows programming options. I know that .net is a framework and MFC is sort of the C++ option for Win32 (right?) but I have no idea what WPF or COM or any of those other TLA mean (beyond a quick Google search anyways).
As an amateur level hobbyist programmer that wants to evolve beyond simple ~100 line console programs to useful GUI-based programs that do interesting things I feel very nervous about hitching my wagon to one particular OS-specific framework. I picked GTK because it has decent cross-platform support (excluding OS X unfortunately) but it concerns me I almost never see programs for Windows written in GTK.
I'm just being paranoid about spending time learning something that isn't useful in a lot of situations. That being said I'm happy that the time spent with GTK at least allowed me to understand the Win32 API on a superficial level.
Sunday, September 25, 2011
Divisor count results for 3 million
The number between 1 and 3 million with the largest number of divisors is 2882880 with 336 divisors.
Execution time was over six hours.
The actual output of the program (with CodeBlocks' own time counting):
---
The largest is 2882880 with 336 divisors
Process returned 0 (0x0) execution time : 23027.710 s
Press any key to continue.
Execution time was over six hours.
The actual output of the program (with CodeBlocks' own time counting):
---
The largest is 2882880 with 336 divisors
Process returned 0 (0x0) execution time : 23027.710 s
Press any key to continue.
Notes on divisors
Project Euler 12 requires counting how many divisors a number has. It only wants positive divisors, and 1 and the number itself count. So, the number 12 has six divisors: 1, 2, 3, 4, 6, and 12.
You're trying to find the first triangle number that has 500 divisors. Well, I'd like to not bother calculating triangle numbers that aren't going to have anywhere near 500 divisors so I want to know how high numbers have to get before they start approaching having 500 divisors.
I wrote this program to loop from 1 to a high number, count how many divisors each number has (and dump it to a text file), report which number had the highest number of divisors, and the number of divisors it had.
I had hoped to graph it out see it generally rise and maybe guess about where it would hit the 500 mark, but I ran it from 1 to a million and it only got to around 200.
The program would eventually get me the answer I want but once the numbers get high my CountDivisors() function starts really slowing down. Now I'm researching faster options using recursion.
This exercise is also revealing how much basic math I've lost over the years. Like the difference between divisors and factors. Divisors will divide a number with no remainder and factors will multiply into the number (which can be broken down into a set of prime numbers). Terminology, woof. I need a fifth grade math book on hand.
EDIT: I set the upper limit of numbers to get divisors from to 3 million. I started it this morning and it's still going. I'm about to head out for the afternoon so I'll leave it running. I think I'll stop it tomorrow morning if it isn't done yet. I got rid of the printf statements since they do indeed slow down the process but now I'm not sure where it's at in the loop.
You're trying to find the first triangle number that has 500 divisors. Well, I'd like to not bother calculating triangle numbers that aren't going to have anywhere near 500 divisors so I want to know how high numbers have to get before they start approaching having 500 divisors.
I wrote this program to loop from 1 to a high number, count how many divisors each number has (and dump it to a text file), report which number had the highest number of divisors, and the number of divisors it had.
I had hoped to graph it out see it generally rise and maybe guess about where it would hit the 500 mark, but I ran it from 1 to a million and it only got to around 200.
The program would eventually get me the answer I want but once the numbers get high my CountDivisors() function starts really slowing down. Now I'm researching faster options using recursion.
This exercise is also revealing how much basic math I've lost over the years. Like the difference between divisors and factors. Divisors will divide a number with no remainder and factors will multiply into the number (which can be broken down into a set of prime numbers). Terminology, woof. I need a fifth grade math book on hand.
EDIT: I set the upper limit of numbers to get divisors from to 3 million. I started it this morning and it's still going. I'm about to head out for the afternoon so I'll leave it running. I think I'll stop it tomorrow morning if it isn't done yet. I got rid of the printf statements since they do indeed slow down the process but now I'm not sure where it's at in the loop.
Subscribe to:
Posts (Atom)