Unofficial Programming Thread

Post » Thu Sep 23, 2010 2:40 pm

My problem with the sieve technique for prime numbers is the "last stretch". To find the last primes close to one billion, I still have to start crossing out all the numbers multiplied by 3 then 5 and so on until the root of billion. Even after buffered writing to stdout, the algorithm still takes over 5 seconds on my test set. I think I should be looking at fractions of a second..
User avatar
Miss K
 
Posts: 3458
Joined: Sat Jan 20, 2007 2:33 pm

Post » Thu Sep 23, 2010 1:47 pm

My problem with the sieve technique for prime numbers is the "last stretch". To find the last primes close to one billion, I still have to start crossing out all the numbers multiplied by 3 then 5 and so on until the root of billion. Even after buffered writing to stdout, the algorithm still takes over 5 seconds on my test set. I think I should be looking at fractions of a second..


Yeah, it shouldn't take that long, not even for a brute force approach.

Sometimes stdout to a console is slowed by the console. Try redirecting to a file and see if that gives a faster result. Also if you're using a garbage-collecting language, the garbage collector slows you down if it kicks in.

I got a solution to the Codechefs PRIME1 problem that ran in 0.05 seconds, including I/O, without using any write technique more optimized than printf. A key to this problem is that you don't need all the prime numbers up to 1 billion. The problem only asks for ten ranges of prime numbers, no more than a hundred thousand at a time. You can do that by sieving all the primes less than the square root of 1 billion (hint: 31607 is the greatest prime less than that), then using those to knock out their multiples in arrays no bigger than 100,000 members (50,000 if you simplify by taking 2 as a special case and only knocking out odd numbers). That is such a small problem that you do not need an optimized sieve like Atkin to do it fast.
User avatar
Claire Vaux
 
Posts: 3485
Joined: Sun Aug 06, 2006 6:56 am

Post » Thu Sep 23, 2010 5:25 pm

Other then CodeChef that I linked earlier, and other then programs that would be useful for only me, anyone know of a site to find project idea or exercises? I have a book but it basically just says "add this code to this program example", where my old book has the reader modify a programbut doesn't provide the code. I want to learn more things and make smething semiuseful, possibly for people other then myself.
User avatar
Michelle Serenity Boss
 
Posts: 3341
Joined: Tue Oct 17, 2006 10:49 am

Post » Thu Sep 23, 2010 6:18 am

Yeah, it shouldn't take that long, not even for a brute force approach.

Sometimes stdout to a console is slowed by the console. Try redirecting to a file and see if that gives a faster result. Also if you're using a garbage-collecting language, the garbage collector slows you down if it kicks in.

I got a solution to the Codechefs PRIME1 problem that ran in 0.05 seconds, including I/O, without using any write technique more optimized than printf. A key to this problem is that you don't need all the prime numbers up to 1 billion. The problem only asks for ten ranges of prime numbers, no more than a hundred thousand at a time. You can do that by sieving all the primes less than the square root of 1 billion (hint: 31607 is the greatest prime less than that), then using those to knock out their multiples in arrays no bigger than 100,000 members (50,000 if you simplify by taking 2 as a special case and only knocking out odd numbers). That is such a small problem that you do not need an optimized sieve like Atkin to do it fast.

Yes, that's what I did. "test.exe < in.txt > out.txt" and timed that. Brute force for the last stretch was faster than sieving with my routine <_<. Using C/C++ as usual, so no garbage collection. I didn't try to send in my initial approach to sieving though.

So first I should calculate the primes up to sqrt(N), where N is the requested upper limit. Keep those, print, if within the limits, and cross out the multiples from the array. The remaining numbers should be the primes. Ok, I can do that.. I guess my algorithm kept doing the same thing over and over again, and then it even kept printing out some obviously even figures. Getting to 0.05 seconds seems a little far fetched right now.
User avatar
Yung Prince
 
Posts: 3373
Joined: Thu Oct 11, 2007 10:45 pm

Post » Thu Sep 23, 2010 2:51 am

Also as you are using C/C++ and I/O is the issue don't use std::endl as it flushes the stream. Use "\n". (I don't know if you knew that :shrug:)
User avatar
Dj Matty P
 
Posts: 3398
Joined: Sat Jun 09, 2007 12:31 am

Post » Thu Sep 23, 2010 4:27 am

Also as you are using C/C++ and I/O is the issue don't use std::endl as it flushes the stream. Use "\n". (I don't know if you knew that :shrug:)


Interesting piece of info. I wasn't aware of that, although I tend to use "\n" a lot anyway as it's generally neater.
User avatar
Nienna garcia
 
Posts: 3407
Joined: Wed Apr 25, 2007 3:23 am

Post » Thu Sep 23, 2010 4:49 pm

Also as you are using C/C++ and I/O is the issue don't use std::endl as it flushes the stream. Use "\n". (I don't know if you knew that :shrug:)

I'm using fputs with a buffer implementation of my own ala strcat, lol. I'm not even including iostream, that would know about endl. Technically I could make it in C by moving variable definitions and getting rid of that std::bitset.. And maybe the so called buffer is not anything good after all and it's just making the whole thing slower. I dunno, but I finally solved the task in 0.23s..
User avatar
Hayley Bristow
 
Posts: 3467
Joined: Tue Oct 31, 2006 12:24 am

Post » Thu Sep 23, 2010 4:02 am

Ooh, so it was my substandard buffer implementation then..

Correct Answer
Execution Time: 0.02
User avatar
Robert Jr
 
Posts: 3447
Joined: Fri Nov 23, 2007 7:49 pm

Post » Thu Sep 23, 2010 5:09 am

Just seriously looked into http://code.google.com/p/angel-engine/, recommended by a SJML. It's clicking, and seems to be exactly what I'm looking for.

What do you guys think about a 2d platformer, based around a caveman with a ropegun? That's my concept so far, while I'm in bed I'll begin on the story and possibly level design. Let's see where my mind takes me.


This isn't programming related, but why haven't any games used the Stone Age as a setting? There's so much to work with, not to mention how hilarious it could potentially be.

The closest I've ever saw was Fallout 2 with the primitives, aside from that there's no other RPG that covers the time period - that I've played.


Glad Angel is working out for you! Let us (the Angel devs) know if you have any questions or problems. There aren't a lot of us, but questions posted to the discussion list get answered pretty quickly.

I always think back to things like Bonk and the prehistorical periods of Chrono Trigger for that kind of stuff. :-)

(PS - I didn't know this thread was a thing. Very cool!)
User avatar
Amber Ably
 
Posts: 3372
Joined: Wed Aug 29, 2007 4:39 pm

Post » Thu Sep 23, 2010 5:33 pm

I am starting to look into making a webpage that uses ASP.NET (using C#) to look up inventory and pricing and display both a full list and a short list. But I want to make a class for all the code dealing with my inventory database.

How do you guys normally name your classes? I can't settle on a name for mine, not that it really matters though but I want it to be something to do with what the class does. That way others don't need to open it to see what I did if I hand off the project.
User avatar
daniel royle
 
Posts: 3439
Joined: Thu May 17, 2007 8:44 am

Post » Thu Sep 23, 2010 1:39 pm

I am starting to look into making a webpage that uses ASP.NET (using C#) to look up inventory and pricing and display both a full list and a short list. But I want to make a class for all the code dealing with my inventory database.

How do you guys normally name your classes? I can't settle on a name for mine, not that it really matters though but I want it to be something to do with what the class does. That way others don't need to open it to see what I did if I hand off the project.


I would call it "Inventory" or "CInventory" in that sort of situation. I'm very creative like that. Or maybe "CInventoryManager" would be more appropriate.
User avatar
meghan lock
 
Posts: 3451
Joined: Thu Jan 11, 2007 10:26 pm

Post » Thu Sep 23, 2010 7:31 am

I am starting to look into making a webpage that uses ASP.NET (using C#) to look up inventory and pricing and display both a full list and a short list. But I want to make a class for all the code dealing with my inventory database.

How do you guys normally name your classes? I can't settle on a name for mine, not that it really matters though but I want it to be something to do with what the class does. That way others don't need to open it to see what I did if I hand off the project.


I name classes according to their purpose: what they contain, what they do, what they depict, etc. "Inventory" for a class that records quantities, cost, and sales of stock in trade is a perfectly good name. "Stockable" for an interface that provides methods to add to and remove from stock is a perfectly good name. Class names are usually nouns, interface names are usually adjectives, and method names are usually verbs, and there should be a good reason for deviating from this convention before doing so.

I consider the practice of prefixing class names (and other names) with signs denoting their type (such as CInventory for an Inventory class, IStockable for a Stockable interface) to be a "custom more honored in the breach than the observance": it gives you no information that an IDE would not present you with, nor any information that you could not determine quickly by inspection; all it does is interfere with attempts to collate names in semantically useful order. Only in languages that have grossly deficient object models, such as Visual Basic, is there any justification for this practice.
User avatar
Janine Rose
 
Posts: 3428
Joined: Wed Feb 14, 2007 6:59 pm

Post » Thu Sep 23, 2010 4:51 am

I consider the practice of prefixing class names (and other names) with signs denoting their type (such as CInventory for an Inventory class, IStockable for a Stockable interface) to be a "custom more honored in the breach than the observance": it gives you no information that an IDE would not present you with, nor any information that you could not determine quickly by inspection;

I agree. It reminds me somewhat of the Hungarian Notation, which in a strongly typed language there is no excuse for using. I was really surpried that Microsoft C# standards (I think) suggest prefixing interfaces with I. :shrug:
User avatar
SHAWNNA-KAY
 
Posts: 3444
Joined: Mon Dec 18, 2006 1:22 pm

Post » Thu Sep 23, 2010 3:04 am

About naming of classes. For my own projects, I'd just capitalize the first letter of the class name and name it so that it's easy to tell what it contains without looking at the definition. At work, I'd just follow whatever the coding conventions were.. The usual practice seems to be to start them classes with a capital C prefix. At some point, when the projects start getting complex, you might consider prefixing the class name with something that tells you the context, the library it comes from etc. Maybe I don't know how to use Visual Studio to see that information directly in one glance.

Other names.. Private class member variables I usually start with a prefix "m_", function parameters with just an underscore "_", local variables without one. Variables in general are prefixed with the type. "l" for long, "s" for strings etc. Arrays are prefixed with an "a". It makes the code quicker and easier to read for me. This way I can also tell where to look for the declaration even if I was out of the IDE looking at a diff report on the changes someone else made to the file. Why is this a bad thing? Maybe it's just an old habit because I've used a similar naming convention for many years.
User avatar
emma sweeney
 
Posts: 3396
Joined: Fri Sep 22, 2006 7:02 pm

Post » Thu Sep 23, 2010 7:53 am

Alright guys I decided not to work on the ASP.NET project and started another one. It is a program that generates or compares MD5 hashes for either files or text strings. It is small (about 13kb), and DOES require .NET 3.0 to run. Would anyone be interested in trying it? It is a very simple interface and I am considering adding a few simple options, such as how many file to show in the recent list or export MD5 sums to text files. Here is a http://i48.tinypic.com/119z2ww.jpg of the current version. Any suggestions as well are welcome.

A few things I am considering:
- limiting amount in the recent view
- export information from recent view
- save/restore recent items
- eliminate convert button and rely on user pressing enter
- eliminate browse button and open the dialog when the text box is clicked

Would anyone be interested in trying this program out? If so I can upload it for you all, I don't want to fully release it until it gets tested and some code cleaned up and changed, but I want testers for it so far.
User avatar
Spaceman
 
Posts: 3429
Joined: Wed May 23, 2007 10:09 am

Post » Thu Sep 23, 2010 4:44 pm

This is in some respects statistics rather than programming, but it is related. I am slowly writing a program that is going to be entered in a competition and as usual it isn't the programming that is causing the problem, but the theory behind it.

I have a set of data which contains multiple variables on how well the user preformed at answering a question, where each question has a set of tags. I need to, when given a set of tags, predict how well the user is going to do at answering the question.

I am assuming that someone has done some work or written a paper on it somewhere, but I have not managed to find anything. Does anyone know of any research or information relating to the problem? I may have just been googling for the wrong terms.



Other names.. Private class member variables I usually start with a prefix "m_", function parameters with just an underscore "_", local variables without one. Variables in general are prefixed with the type. "l" for long, "s" for strings etc. Arrays are prefixed with an "a". It makes the code quicker and easier to read for me. This way I can also tell where to look for the declaration even if I was out of the IDE looking at a diff report on the changes someone else made to the file. Why is this a bad thing? Maybe it's just an old habit because I've used a similar naming convention for many years.

The only thing I do is start class variables with an m, mainly because in C++ you don't require 'this->'. (Just 'm' as can't stand underscores as they are I don't like typing them.). I have never had an issue with knowing what type a variable is as I can (most of the time) manage to infer it from the variable name. This is probably because I only really either read my own code or very well written code (Ogre3D etc). Given the "professional" code I have worked with, I expect that will change.
User avatar
m Gardner
 
Posts: 3510
Joined: Sun Jun 03, 2007 8:08 pm

Post » Thu Sep 23, 2010 6:38 pm

This is in some respects statistics rather than programming, but it is related. I am slowly writing a program that is going to be entered in a competition and as usual it isn't the programming that is causing the problem, but the theory behind it.

I have a set of data which contains multiple variables on how well the user preformed at answering a question, where each question has a set of tags. I need to, when given a set of tags, predict how well the user is going to do at answering the question.

I am assuming that someone has done some work or written a paper on it somewhere, but I have not managed to find anything. Does anyone know of any research or information relating to the problem? I may have just been googling for the wrong terms.

First thing I thought of when reading this was predictive data anolysis but I am not sure if you already tried to look that up or if that is really even what you mean. Could be a start though.
User avatar
DAVId Bryant
 
Posts: 3366
Joined: Wed Nov 14, 2007 11:41 pm

Post » Thu Sep 23, 2010 10:59 am

First thing I thought of when reading this was predictive data anolysis but I am not sure if you already tried to look that up or if that is really even what you mean. Could be a start though.

Ah, using those terms gives me a better set of results from Google. Wikipedia seems to have a fairly http://en.wikipedia.org/wiki/Predictive_anolytics#Statistical_techniques of options of statistical methods for prediction. Thanks :)
User avatar
Kortknee Bell
 
Posts: 3345
Joined: Tue Jan 30, 2007 5:05 pm

Post » Thu Sep 23, 2010 4:13 pm

Ah, using those terms gives me a better set of results from Google. Wikipedia seems to have a fairly http://en.wikipedia.org/wiki/Predictive_anolytics#Statistical_techniques of options of statistical methods for prediction. Thanks :)

Not a problem, I wasn't even sure that was what you were looking for, so glad I could help.

I recently read about SHA1 and SHA2 sums that could be used to check files and was trying to think of it would be a good idea to add to my litle program. I am not sure if I want to release it completely as opposed to just a few people testing or not. Do you guys think just adding it in and making my program have more feature be better then an independant application? I also got a suggestion to make an assembly of all my conversions to the various hashes, even if the namespace exists it would be easier if I ever need to use the code again, or if someone else wants to without worrying about the exact code to do it.
User avatar
JLG
 
Posts: 3364
Joined: Fri Oct 19, 2007 7:42 pm

Post » Thu Sep 23, 2010 3:01 pm

I have a set of data which contains multiple variables on how well the user preformed at answering a question, where each question has a set of tags. I need to, when given a set of tags, predict how well the user is going to do at answering the question.

Sounds like http://en.wikipedia.org/wiki/Bayesian_inference to me. It's pretty simple to implement and produces quite decent results.
User avatar
Dina Boudreau
 
Posts: 3410
Joined: Thu Jan 04, 2007 10:59 pm

Post » Thu Sep 23, 2010 1:08 pm

Sort of a programming question relating to job hunting:

If I am just looking for any job in the IT industry currently, should I include any small applications I write as a 'portfolio' just to show other skills I have? I am not that advanced with my programming and I still look up some advanced stuff to code it but I have the basic understanding and am always learning. I was thinking of just keeping a folder of .zip files of the source code for various programs and printing some of them out to keep as well. Would this be advisable if I don't plan to pursue a programming career?
User avatar
Nicole Coucopoulos
 
Posts: 3484
Joined: Fri Feb 23, 2007 4:09 am

Post » Thu Sep 23, 2010 7:58 am

Sort of a programming question relating to job hunting:

If I am just looking for any job in the IT industry currently, should I include any small applications I write as a 'portfolio' just to show other skills I have? I am not that advanced with my programming and I still look up some advanced stuff to code it but I have the basic understanding and am always learning. I was thinking of just keeping a folder of .zip files of the source code for various programs and printing some of them out to keep as well. Would this be advisable if I don't plan to pursue a programming career?


It's not really necessary if you aren't going for a programming job, but it wouldn't hurt. I suppose it at least shows that you like to try and learn new things which is a good quality to have.
User avatar
jessica robson
 
Posts: 3436
Joined: Mon Oct 09, 2006 11:54 am

Post » Thu Sep 23, 2010 5:15 am

I need some interface suggestions for the above mentioned MD5 generator/comparer. I am thinking of adding both SHA1 and SHA256 sums to it but I am not sure how to modify my interface to differenciate between them in the recent window. I was thinking either color code them with a legend right above the box to show which is which, adding a new column to show the type, or just put it in parenthesis next to where it displays if a file or text was processed.

Any suggestions? I am trying to not make the interface too much bigger right now it is 480x330, it isn't a requirement just a design decision.
User avatar
zoe
 
Posts: 3298
Joined: Sun Nov 12, 2006 1:09 pm

Post » Thu Sep 23, 2010 7:01 pm

I don't really know in what way you are going with it, or what technical limitation you might have, or what's the target user of this application, but.. You could rename the current column and add new columns by names SHA1 and SHA256. If you really don't want to make the view wider, one way would be to allow selection of which one to calculate and show only that. Maybe a radio button group.

Does it make a difference to the user whether or not the file was text?

Interface suggestions:
Directory view
- You can select a directory and your program would calculate the hashes on either each file within the directory automatically or for the selected files on the push of a button. The few hashes would displayed on the same line as the corresponding file name on separate columns. Make the view scalable in height. Then you can expand it by allowing export of selected hashes to a text file with the file names. Or you might want to import the same file to see if some files by the same name are different or changed in the same directory.
User avatar
jesse villaneda
 
Posts: 3359
Joined: Wed Aug 08, 2007 1:37 pm

Post » Thu Sep 23, 2010 5:01 am

Maybe also add a way of comparing the hash of the file and another hash. For example a lot of the time if I download a file there is also a hash of the file given that you can use to check if the file download without errors.
User avatar
Danii Brown
 
Posts: 3337
Joined: Tue Aug 22, 2006 7:13 am

PreviousNext

Return to Othor Games