Search found 100 matches

Fri Oct 30, 2009 2:17 pm UTC
Forum: Coding
Topic: Finding Prime Numbers With C++
Replies: 35
Views: 2886

Re: Finding Prime Numbers With C++

Now, let's do a little optimization ! In isPrime(), you do if (int div = 2; div <= num/2; div++) { ... } Now, if 2 is not a divisor of num, basic arithmetic tells us that no even number is a divisor of num. So, first, check if num != 2 and num % 2 != 0, if that's the case, you can test only agains o...
Fri Oct 30, 2009 1:17 pm UTC
Forum: Coding
Topic: Finding Prime Numbers With C++
Replies: 35
Views: 2886

Re: Finding Prime Numbers With C++

You corrected your test to find a divisor in the isPrime function, but not the one in your main function.
Wed Oct 28, 2009 7:59 am UTC
Forum: Coding
Topic: Project Euler
Replies: 411
Views: 157162

Re: Project Euler

I must say that I'm skeptical concerning the n % 6 == 1 test. Can you say how you got that condition ?

I used a slightly optimized brute force and it worked just well (in 2.3s to be precise, but seeing the code, I think it still can be reduced).
Tue Oct 13, 2009 12:17 pm UTC
Forum: Coding
Topic: Coding: Fleeting Thoughts
Replies: 9979
Views: 1977244

Re: Coding: Fleeting Thoughts

Damn, got a real bad case of "What the hell did I wrote ???"... Just looked what I wrote for to solve PE#43, and I haven't got the slightest idea of what I was thinking at the time...

Hmmm... On closer inspection, I might know what I wanted to do, but hell if I know how/why it works !
Mon Sep 07, 2009 1:47 pm UTC
Forum: Coding
Topic: Coding: Fleeting Thoughts
Replies: 9979
Views: 1977244

Re: Coding: Fleeting Thoughts

Foreach is really great and should be used whenever possible. The only drawback (in C#, at least) : you must not alter the collection you're iterating through. Back to the good ol' for loop, in this case.
Mon Jul 20, 2009 8:56 am UTC
Forum: Coding
Topic: [Solved]C# Random number generation issues
Replies: 3
Views: 1188

Re: C# Random number generation issues

Wild guess, but : This method draws an image using its physical size, so the image will have its correct size in inches regardless of the resolution (dots per inch) of the display device. For example, suppose an image has a pixel width of 216 and a horizontal resolution of 72 dots per inch. If you c...
Fri Jul 17, 2009 3:17 pm UTC
Forum: Coding
Topic: The "IT DOESN'T WORK!" thread
Replies: 1958
Views: 556713

Re: The "IT DOESN'T WORK!" thread

One of my other guesses was that R(x) might overflow a char, but even so, the (*f).write(&r, 1) should correct such a problem... Unless a char can go over 255 (it can in C#, but in C++, I doubt so... except if it can store unicode, but that just a blind guess, I never use C++, so I'm not familia...
Fri Jul 17, 2009 2:37 pm UTC
Forum: Coding
Topic: The "IT DOESN'T WORK!" thread
Replies: 1958
Views: 556713

Re: The "IT DOESN'T WORK!" thread

Looks like an off by one error to me. Due to rounding errors, the loop calculating the average color could go off one too far (the adding of the grain_* n times might add to just a little less than pixel_*, so, it loops one more time, going over the limit. But I might be wrong.
Fri Jul 10, 2009 8:51 am UTC
Forum: Coding
Topic: Project Euler
Replies: 411
Views: 157162

Re: Project Euler

@JBJ : as Berengal and I said, some numbers can be written as different sums, thus you have to check for duplicate results. I proposed a solution in my spoiler, Berengal proposes to use a set.

@Berengal : this one liner seems so simple... I wish it was this easy to do that in C#...
Thu Jul 09, 2009 8:35 am UTC
Forum: Coding
Topic: Project Euler
Replies: 411
Views: 157162

Re: Project Euler

I didn't want to work, so I decided to try PE#87. Quite easy if you don't do stupid errors in your code, but the worst part ? Checking for duplicate results ! Checking at insertion ? Slow as hell !!! Eventually, I remebered that : Sort() is your friend ! Insert every single result in a List<...
Tue Apr 07, 2009 10:09 am UTC
Forum: Coding
Topic: IsNullOrEmpty
Replies: 1
Views: 636

Re: IsNullOrEmpty

I've never encountered a single problem with string.IsNullOrEmpty(string) in .Net 2.0 in any of the numerous production applications we have at work.
Thu Feb 05, 2009 10:34 am UTC
Forum: Coding
Topic: Languages used for....?
Replies: 21
Views: 2546

Re: Languages used for....?

Web development in ASP.NET/C#, MS SQL Server (T-SQL), SSAS (MDX).
Tue Feb 03, 2009 4:30 pm UTC
Forum: Coding
Topic: Rebuild executable from hexdump
Replies: 15
Views: 1939

Re: Rebuild executable from hexdump

It depends on how it is stored, but it's a simple matter of parsing your dump to write the actual values in a file (i.e. transforming a string of the form "XY" in a byte of value 0xXY)...
Sat Jan 10, 2009 11:11 pm UTC
Forum: Coding
Topic: Accessing a file on the internet?
Replies: 13
Views: 1456

Re: Accessing a file on the internet?

There are quite a few good posts about such a program in this very sub-forum. But for your exemple, we're looking at HTTP : make a request to each page (http://xkcd.com/xxx), parse it to identify the location of the image (search for the link written in the page or for the correct img element, shoul...
Sat Jan 10, 2009 10:34 pm UTC
Forum: Coding
Topic: Accessing a file on the internet?
Replies: 13
Views: 1456

Re: Accessing a file on the internet?

What exactly do you mean by "reads a file on the internet the way it accesses things on my own computer" ? Reading a file on the internet (or any other network for that matter) requires a protocol : HTTP, FTP, SSH, NetBIOS, etc. ... And the corresponding "server" on the other end...
Tue Dec 09, 2008 9:17 am UTC
Forum: Coding
Topic: About the computer programming industry?
Replies: 17
Views: 2036

Re: About the computer programming industry?

I'm in the computer programming industry since 4 years, but not in the US. What I've seen, though, is that the problem is more because of the current context of economy (crisis, some speak of recession), so, in general, employment is going bad, be it computer programming or any scientific field, I t...
Mon Dec 08, 2008 9:20 am UTC
Forum: Coding
Topic: Does anyone else find C#'s treatment of pointers hilarious?
Replies: 9
Views: 1560

Re: Does anyone else find C#'s treatment of pointers hilarious?

Well, I'm no C# guru either, but my understanding is that if you have a reference, the runtime and the GC will make sure that when you use it as is, you'll always point to your object. But if you decide to use a pointer (eg : char* chRef = &str.m_firstChar (although, m_firstChar being private, y...
Fri Dec 05, 2008 3:48 pm UTC
Forum: Coding
Topic: Munching triangles: Why am I seeing this?
Replies: 3
Views: 1124

Re: Munching triangles: Why am I seeing this?

Interesting ! I tried a little in C# (diverted me from boring work). I did the "classics" (xor, and, or) and two for the fun : y = cos(x*t) and y = x**t. (Everything back in integer range of [0; 255] for ease of drawing and varied the alpha with "T" to "show" the iterat...
Fri Dec 05, 2008 10:27 am UTC
Forum: Coding
Topic: Does anyone else find C#'s treatment of pointers hilarious?
Replies: 9
Views: 1560

Re: Does anyone else find C#'s treatment of pointers hilarious?

Use of pointers in C# IS unsafe because of the GC, which might move your pointer around if it thinks it's necessary for a more optimized space organization (thus the keyword fixed which prevent the GC from moving it (example of use : string.Copy(string))).
Thu Dec 04, 2008 1:37 pm UTC
Forum: Coding
Replies: 2
Views: 822

Mon Nov 24, 2008 3:38 pm UTC
Forum: Coding
Topic: Making random numbers C++
Replies: 41
Views: 4260

Re: Making random numbers C++

http://imgs.xkcd.com/comics/random_number.png I didn't read everything, but my guess is that a simple rand()%6 + 1 is quite sufficient (cf. first answer which is a little more generic), the "~ 1/5000" bias mentioned by evilbeanfiend is, IMHO, negligeable for everyone's need. Plus, the [1,...
Fri Nov 07, 2008 1:19 pm UTC
Forum: Coding
Topic: Coding: Fleeting Thoughts
Replies: 9979
Views: 1977244

Re: Coding: Fleeting Thoughts

For this kind of cases, I do something along the lines of myList[i % myList.Count], but only because I work with the rigid C#
Fri Nov 07, 2008 8:50 am UTC
Forum: Coding
Topic: Coding: Fleeting Thoughts
Replies: 9979
Views: 1977244

Re: Coding: Fleeting Thoughts

Damned mathematics ! But surely I should have known that... I tackled this kind of problems at school... But it's been sooo long ago... (ok, only 4 years ago, but when your work doesn't require a tenth of what you learned, it's hard to maintain the edge.) Well, not that I have a use of such a thing,...
Fri Nov 07, 2008 8:18 am UTC
Forum: Coding
Topic: Coding: Fleeting Thoughts
Replies: 9979
Views: 1977244

Re: Coding: Fleeting Thoughts

In C#, what if an ArrayList contained itself ? Surely, it shouldn't bother the framework, and truly it didn't ! But what if I wanted type security, i.e. used a List<T> ? I would need a List of itself ! but then, I would need to write List<List<List<List<......>>>>, surely, this is problematic... Let...
Fri Oct 31, 2008 9:10 am UTC
Forum: Coding
Topic: Project Euler
Replies: 411
Views: 157162

Re: Project Euler

I believe you can declare in C an "unsigned long long" which should be a 64 bits unsigned integer which should range from 0 to 18.446.744.073.709.551.615 (more than 1.8*10^19) if I'm correct which should be enough for most of the problems (but not always...)
Thu Oct 30, 2008 12:52 pm UTC
Forum: Coding
Topic: Project Euler
Replies: 411
Views: 157162

Re: Project Euler

If interpreted correctly, the following hint gives you the solution... Or at least the one I used...
Spoiler:
pf(84) == pf(42) == pf(21) == pf(7)
Wed Oct 22, 2008 12:21 pm UTC
Forum: Coding
Topic: next item in loop, in subloop?
Replies: 9
Views: 1615

Re: next item in loop, in subloop?

C# : List<Article> lArticles = GetArticleList(); List<string> lKeywords = GetKeywords(); List<Article> lArticlesOK = new List<Article>; foreach (Article a in lArticles) { foreach (string k in lKeywords) { if (Regex.IsMatch(a.Title, k)) { lArticlesOK.Add(a); break; // exit the innermost loop, here th...
Thu Oct 16, 2008 9:38 am UTC
Forum: Coding
Topic: Project Euler
Replies: 411
Views: 157162

Re: Project Euler

Well, I'm sure it's one of those problem where you actually build the good solutions and count them instead of checking all the possibilities. I'm sure the symetry "trick" still apply (you only have to build half the possibilities). Good luck to those willing to try this one
Thu Oct 16, 2008 8:37 am UTC
Forum: Coding
Topic: Project Euler
Replies: 411
Views: 157162

Re: Project Euler

Nope, didn't think about, seems a bit too complex for my little head... But it's a binary tree of depth 70. A path can be represented by an array of 70 bits (clockwise or anticlockwise choice). Symetry should help a bit (if you have a path p, its bitwise "not" should be a valid solution to...
Wed Oct 15, 2008 1:17 pm UTC
Forum: Coding
Topic: Project Euler
Replies: 411
Views: 157162

Re: Project Euler

Anarchy golf has a context has to the shortest program to resolve certain problems. Yours looks vaguely like Show the way.
Wed Oct 15, 2008 1:05 pm UTC
Forum: Coding
Topic: Mysterious Runtime Errors
Replies: 3
Views: 995

Re: Mysterious Runtime Errors

IMHO, all this index juggling is dangerous and can result in some problems (it took me hours to find a problem in an implementation of a quicksort of mine because of all the index juggling). Anyway, I would to point a flaw in the logic of your programm if I understood what you tried to explain. Let'...
Fri Oct 10, 2008 10:13 am UTC
Forum: Coding
Topic: How to approach difficult problems?
Replies: 25
Views: 3130

Re: How to approach difficult problems?

Here how I try to solve PE problems : ⋅ Bruteforce !!! Try all the parameters space and check each one for the property. Will work on early problems. Eg : on problem 5, for each number starting from 1, check if it's evenly divisible by all numbers from 1 to 20. ⋅ Try to reduce th...
Tue Oct 07, 2008 8:23 am UTC
Forum: Coding
Topic: XKCD doesn't comment his code?
Replies: 48
Views: 7447

Re: XKCD doesn't comment his code?

In my work, I find myself commenting in the following cases : ⋅ Beginning of a project with the project lead insisting on having comments in the code. At this point, it's mostly describing what the function do (what will show in the generated documentation and tooltips in Visual Studio), a...
Mon Sep 22, 2008 2:21 pm UTC
Forum: Coding
Topic: Visual basic 2008 help me
Replies: 7
Views: 1401

Re: Visual basic 2008 help me

When you are done with that project, I highly recommend something with a little more ambition . One of these things is not like the others. ETA you can add C# to the list since he evidently already has visual studio, and C# is just a rip off of java. IMHO, those two posts are just plain holy wars.....
Thu Aug 14, 2008 12:09 pm UTC
Forum: Coding
Replies: 34
Views: 4642

My bad, I was wrong. I thought that log was the log base 10, not the natural log. But I'll hold to what I say, and once again : do as you wish ! It's your code, your way of learning. I'm just here to give my (humble ?) opinion for what it's worth. I could be totally wrong so I should not be taken fo...
Thu Aug 14, 2008 11:34 am UTC
Forum: Coding
Replies: 34
Views: 4642

I'm not saying that you must use a vector, but that I would recommend the use of one. My point of view is that it's not a question of speed (the vector might be a little slower due to dynamic memory allocation), but rather of memory efficiency. Exemple : n = 104744. There are 10001 primes below that...
Thu Aug 14, 2008 10:01 am UTC
Forum: Coding
Replies: 34
Views: 4642

if(!w) is the same thing as if(w == 0). Yes, but if I type -1 (because I'm a big bad boy), you'll output "garbage" (whatever is stored before your array in the heap) if not a segfault (unlikely, but who knows). That's why I encourage to check against negative numbers (w is a signed intege...
Thu Aug 14, 2008 9:16 am UTC
Forum: Coding
Replies: 34
Views: 4642

DISCLAIMER : I'm no specialist and my mindset is not to flame your work but to point out what (and how) you could do better in my very humble opinion, for what my experience is worth (not much in C++ and only a 3-4 years as a programmer in my daily job), so please, don't consider this as a way to de...
Fri Aug 08, 2008 2:50 pm UTC
Forum: Coding
Topic: Good programming music
Replies: 93
Views: 15578

Re: Good programming music

Any music already running in the background (mostly Rhapsody, Nightwish, Sonata Arctica, Infinity, ..., but also Madness, Tears for Fears, classical music, buddhist chants, etc., etc., etc.) on shuffle or not depending of what I wanted to listen, the trick being that after 2 or 3 minute, I don't &qu...
Fri Aug 08, 2008 11:53 am UTC
Forum: Coding
Topic: Coding: Fleeting Thoughts
Replies: 9979
Views: 1977244

Re: Coding: Fleeting Thoughts

And here I was thinking that brute force couldn't be that bad for this one. I checked what I wrote for this one, and though I didn't used the best solution, it only took 65ms to compute. Then I tried the worst algorithm I could think of. It is that bad... Well, at least, it takes more than 2 minutes...