Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Given two very large numbers a and b where a < b, the problem is to find the following sum:
a + (a + 1) + (a + 2) + ... + (b - 2) + (b - 1) + b
The numbers a and b can be very very large (can contain millions of digits).
As these are very large numbers, programming languages cannot find the sum using integer data types.
So, the only option is to use strings.
What is the most efficient way to do so?
programming languages cannot find the sum using integer data types. So, the only option is to use strings.
This is false. You can't represent a million-digit integer with a 32-bit or 64-bit int, sure, but it doesn't follow that the best option then is to use strings. You would use a library for dealing with large numbers, see "arbitrary precision arithmetic" -- which lets you represent numbers as large as available memory allows.
In the case of this sum, if m = b-a, you want a*(m+1) + sum(1 ...m) . The sum can be calculated with one multiplication via Gauss's formula. That multiplication can be done in O(n^1.47) where n is the number of digits, the various addition operations are O(n), and I assume division by 2 can be done fast, so the whole time complexity is the time complexity of the multiplications.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm working on a project but I've gotten stuck. I need to make a Pascal program capable of converting a number of any base (2-16) to decimal (10). My problem however is that I can't use things like array/string/readkey/type, etc. Basically I'm stuck with only the most basic functions like repeat/while/for/if/case, etc. My problem arises from how I need to input the variable. It needs to be:
-"base:number."
-"answer in base 10"
For example
-16:123.
-291
I can't separate the base from the number using ":". I'm also not sure of how to separate the numbers. I thought about using Ord which seems to be the only way but I have no idea where to put it, or how to write it. Any ideas?
You don't need arrays or any of that stuff. The exercise wants you to apply knowledge of two things: how to process input one character at a time, recognizing semaphores (the colon ':'), and understanding of how the digits of a number are related to its base.
The radix of a number is not an intrinsic quality of a number -- a 7 is a 7 is a 7 no matter what radix you represent it in. The radix is a textual, human-readable characteristic of a number. You have already learned how to handle polynomials in school:
567 → 5×10² + 6×10¹ + 7×10⁰
That 10 in there is the radix → base 10. If we were to use hexadecimal (base 16) the radix is 16:
567₁₆ → 5×16² + 6×16¹ + 7×16⁰
The final trick is to understand how to compose and decompose numbers using the radix via multiplication and remainder operations. Let's rewrite that polynomial to make it more obvious:
567₁₆ → 5×16×16 + 6×16 + 7×1
That five is there in the third-from-the-right position because we multiplied it by 16 two times. The 6 is in the second-from-the-right position because we multiplied it by 16 one time. And the 7 is in the rightmost position because we multiplied it by 16 zero times. In code, that's:
n := 0;
n := n * 16 + 5;
n := n * 16 + 6;
n := n * 16 + 7;
writeln( 'n = ', n );
For your specific assignment, the first number (before the colon ':') is always in base 10. The second number (after the colon ':') uses the radix given by the first number.
Good luck!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
Given a list of 2n-1 numbers: all between 1 to n, all but one occur twice. Determine the number that occurs only once. Multiple ways preferred.
I think the problem is at fault, how can you determine which number without knowing the list of numbers?
[O(1) space, O(n) time]: Just take the XOR of all the numbers. Since all the numbers occur two times except one, XOR of those numbers will be zero and the single occurring number will be the result.
[O(1) space, O(n) time]: As said by user3386109 in comments, we can sum all the given numbers and compare that to the sum of numbers in the range [1, n] which will be n*(n+1) (since all numbers are supposed to occur twice). The difference of the two numbers is the answer.
[O(n) space, O(n) time]: Create an array of size n and keep the count of all the elements in the array at their corresponding positions. At the end, traverse the array, and find the number whose count is only 1.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am aware that computers use the shift and add method to multiply two numbers.
The bit wise shift multiplies and divides by the powers of two. This operation is faster than a multiply instruction.
Multiplication by a constant and division by a constant can be implemented using a sequence of shifts and adds or subtracts.
((x << 2) + x) << 1 // Here 10*x is computed as (x*2^2 + x)*2
(x << 3) + (x << 1) // Here 10*x is computed as x*2^3 + x*2
But is there a faster algorithm to do so?
Thx for the help :)
Compilers use whatever combination of native assembly instructions is most efficient.
When multiplying by a constant, compilers will choose direct multiplication or shifts based on the number of cycles and binary code size each strategy costs. It's typically a job for low level optimizer, that requires exact knowledge of the processor the code is running on.
You won't beat a compiler at this game.
For really large ns (>1000 digits roughly), there are specialised algorithms, in increasing order of strength they are: Karatsuba, Toom-Cook and finally the monster of 100000 digit multiplications: Schönhage-Strassen.
These perform poorly on smaller numbers due to their overheads but asymptotically they're far better than naive multiplication.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How to check if a number is a power of 5?
I could think of below algorithm. Is there way to improve it? Any mathematical trick?
First check if last digit is 5.
If last digit is 5; divide it by 5.
If result of division is 1, then number is power of 5.
Else check if division result itself is power of 5 (i.e. go to step 1 with result as number).
You don't need to look at individual digits, you can just do it like this:
n = (int)(log(x) / log(5)); // get n = log5(x), truncated to integer
if (pow(5, n) == x) // test to see whether x == 5^n
// x is a power of 5
LIVE DEMO
There are only few power of five that fit in int/long range, so you just need to generate all of them and check one by one (less than 60 numbers), using a HashSet will have O(1) time complexity
Successive division until you reach the number undivided by 5 and check whether the result is equal to 1, isn't bad solution. It take log_5(n) operations, so it's O(lg n), it's very good time. For 9094947017729282379150390625 it's only 40 operations!
What I would do is first create an array of numbers that are powers of 5. You could use a range, and then say, for each value in the range, take that value to the fifth power, and push the new value into the array.
You would then find if n, the number you are looking for is included in the array.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I was asked to give an algorithm that was supposed to be O(n(log(k)))
k is the number of arrays and n is the total number of elements in all of these. I had to sort the arrays.
Minus the details I came up with an algorithm that does the job for in klog(k) times the total number of elements. i.e. O(nk(log(k)))
Also in this case k is much smaller than n so it wont be n^2(logn) (in case k and n were almost same)right?
Well, no, it's not the same. If k is a variable (as opposed to a constant) in the complexity expression then O(nk(log(k))) > O(n(log(k))).
That is because there is no constant C such that Cn(log(k)) > kn(log(k)) for every n, k.
The way you describe the question both k and n are input parameters. If that is the case then the answer to your question is
'No, O(n*k *log(k)) is not the same as O(n*log(k))'.
It is not that hard to see that the first one grows faster than the second one, but it is even more obvious if you fix the value of n. Consider n begin a constant say 1. Than it is more obvious that O(k*log(k)) is not the same as O(log(k)).