Why converting from base 10 to base 2 is considered slow? [duplicate] - algorithm

This question already has an answer here:
Computational complexity of base conversion
(1 answer)
Closed 7 years ago.
As stated here it is quadratic, but why?

I think the quadratic part is reading the integer from text. The standard algorithm looks like this:
v = 0
for each digit:
v = v * 10 + digit
It looks like this is just O(n) on the number of digits, but if you are working with arbitrary precision integers like this problem then the multiplication by 10 is also O(n), making the whole thing O(n^2).

Related

Comparing two operations/algorithms (A and B) and determining which one is slower of the two [duplicate]

This question already has answers here:
Comparing two algorithms A and B and determining which one is the slower of the two [closed]
(2 answers)
Closed 2 years ago.
I'm not asking for the answer, but simply just some guidance on how to carry out the following question...
" Algorithm A requires 5n^2 + 10 operations and Algorithm B requires n^2 + 10n operations. Which is the slower of the algorithms and can you conclude about the time requirements for the two algorithms when n is small and when n is large? "
Any guidance is appreciated. Thanks.
A first reaction could be to draw the two functions.
There are online tools for this, so pick one like here:
It looks like for all positive x:
5x2 + 10 > x2 + 10x
So then the second reaction could be to prove that first impression with basic mathematics:
5x2 + 10 > x2 + 10x
⇔ 4x2 - 10x + 10 > 0
The discriminant for the equality is negative, so there are no x for which:
4x2 - 10x + 10 = 0
And as for x=0 the earlier inequality holds, it holds for all x (even negative, but that is irrelevant).
With this information you can make statements about relative speed of the two algorithms.

(Pascal) Read and use a variable one number/character at the time [closed]

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!

What does the exclamation mark mean in big-o, i.e. O(X!)? [duplicate]

This question already has answers here:
Example of a factorial time algorithm O( n! )
(4 answers)
Closed 7 years ago.
I've seen examples of big-o expressed as O(X!) but I am not sure what this means, according to the accompanying charts it is very slow.
Can someone give an example of an O(X!) algorithm?
Thanks
It means factorial. It is the product of the numbers from 1 to X. So by example, 5! = 1 * 2 * 3 * 4 * 5 = 120.
! stands for "factorial".
X! is the product of the numbers from 1 to X.

Determine if sum is a power of 2 [duplicate]

This question already has answers here:
How to check if a number is a power of 2
(32 answers)
Closed 8 years ago.
What is the fastest (in asymptotic worst-case time complexity) algorithm for determining if a sum of arbitrary positive integers is a power of two?
One cute bit twiddling trick is to test if x&(x-1) is equal to 0.
Note that you need to decide what to do if x is equal to 0, this test will mark 0 as a power of 2 so you may want an exception for this case.
Subtract 1 from the sum and perform a bitwise AND with the original number. Powers of 2 will have a result of 0.

Permutation of a numbers in array to sum a number [duplicate]

This question already has answers here:
How to count possible combination for coin problem
(18 answers)
Closed 8 years ago.
I have given an array.And i want to find the all permutation of an array so it sum to a specific numbers.ExampleArray a =[2,3,5 ,1] Target = 8`Solution: [2,2,2,2] ,[5,3] ,[3,3,2] ,[5,2,1] and all possible combinationPlease provide me a approach to solve this the problem , the problem i am facing how to handle the repetition of the elements.Target is a large number of 10^6.
I think it is same asThis theory
You are facing a typical Subset Problem. The worst case complexity of this problem is exponential no matter how you put it. You might find good polynomial-time approximations that work wonders for average case though.

Resources