Is doing a square root once slower than squaring n times? [closed] - c++

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I was just looking at some algorithms for prime numbers and came across this:
for(int i=2;i*i <= n;i++)
{/*assume no operations here*/}
I was just wondering if the above loop will be faster than the following or not?
int x=sqrt(n);
for(int i=2;i<=x;i++)
{/*nop*/}

It depends on the value of n, of course. Anyway, sqrt() is not guaranteed to give you the right result: due to rounding reasons, you might end up with a value of x which is one less than expected and ruin the algorithm. Rather than going for a micro-optimisation, I would stick to correctness here and use the original version, which is guaranteed to give correct results.

Related

How closely related are aspect-oriented programming to macros? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I've heard the term 'aspect oriented programming' tossed around for a long time... I'm still confused... However, it seems to me that the general definition of an aspect is that you can take an existing program, annotate it using an 'aspect' of some sort and have it produce an additional behavior or something completely different. It kinda smells like a macro to me. I'm wondering if there are any similarities/differences as well as any informative links on this matter.

addition vs comparison [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Let say I have an array of 1,000,000 elements with about 90% of 0s and 10% of 1s.
In order to count of 1s, I can do
sum=0;
for(int i=0;i<size;i++) {
sum+=x[i]
}
But I thought maybe comparison is cheaper than addition so this would be better.
sum=0;
for(int i=0;i<size;i++) {
if(x[i]==1)
sum++;
}
But I am not sure. Which one is faster?
It is hard to say which one is going to be faster without trying it, but a even a slightly slower instruction without a branch will usually be faster due to pipelining and branch prediction.
In your case, the branch predictor will be wrong 90% of the time, reducing the speed quite a bit.

How helpful is memorizing Clojure.core [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I've used Clojure for about 2 years now (having used scheme/lisp before that).
I'm getting to the point where I feel like I'm not learning more clojure "by osmosis" and am considering using a conscious effort to memorize the function names in Clojure.core
Question:
Has anyone else done this? If so, has it been a significant productivity boost?
I'm afraid that just having a function memorized is not enough to spot the need to use it when it is needed. It is better to learn in context - for example by solving the 4clojure problems and then looking at solutions by users with high scores. Once you have a function in context, then you can memorize it.

Why are the bits inverted in one's complement? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
When storing a negative number with one's complement before you add the 1 for two's complement, why are all the bits other than the sign inverted? I suppose It would just be simpler if the only thing different was the sign. The only reason I can think of is it somehow make it easier for the computer.
Because that what one's complement is defined to do. See http://en.wikipedia.org/wiki/Signed_number_representations
See, for example, http://en.wikipedia.org/wiki/One%27s_complement, or other sources a quick google can give you.
Basically, yes, it makes addition and subtraction easier to implement compared to sign magnitude numbers (though 2's complement makes maths even easier).

How do I represent a B-tree as a two-dimensional array? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
What resources are available where I can learn how to represent a b-tree using a two-dimenstional array? Searching on Google did not provide any fruitful results.
Ignoring the reasons why you might want to do this, because no one would recommend it, which explains why Google doesn't have much on the subject, the trick is to use indexes into the array in place of pointers.
Then you have one dimension of the array representing nodes in the tree, and the other dimension representing child nodes.
It's related to the problem you would solve if you had to write out a btree to disk, where the disk is essentially a one-dimensional array.