How to check if number is summation of powers of 5 [closed] - algorithm

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.

Related

(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!

Which number appeared once? [closed]

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.

Powers of a half that sum to one [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 8 years ago.
Improve this question
Call every subunitary ratio with its denominator a power of 2 a perplex.
Number 1 can be written in many ways as a sum of perplexes.
Call every sum of perplexes a zeta.
Two zetas are distinct if and only if one of the zeta has as least one perplex that the other does not have. In the image shown above, the last two zetas are considered to be the same.
Find all the numbers of ways 1 can be written as a zeta with N perplexes. Because this number can be big, calculate it modulo 100003.
Please don't post the code, but rather the algorithm. Be as precise as you can.
This problem was given at a contest and the official solution, written in the Romanian language, has been uploaded at https://www.dropbox.com/s/ulvp9of5b3bfgm0/1112_descr_P2_fractii2.docx?dl=0 , as a docx file. (you can use google translate)
I do not understand what the author of the solution meant to say there.
Well, this reminds me of BFS algorithms(Breadth first search), where you radiate out from a single point to find multiple solutions w/ different permutations.
Here you can use recursion, and set the base case as when N perplexes have been reached in that 1 call stack of the recursive function.
So you can say:
function(int N <-- perplexes, ArrayList<Double> currentNumbers, double dividedNum)
if N == 0, then you're done - enter the currentNumbers array into a hashtable
clone the currentNumbers ArrayList as cloneNumbers
remove dividedNum from cloneNumbers and add 2 dividedNum/2
iterate through index of cloneNumbers
for every number x in cloneNumbers, call function(N--, cloneNumbers, x)
This is a rough, very inefficient but short way to do it. There's obviously a lot of ways you can prune the algorithm(reduce the amount of duplicates going into the hashtable, prevent cloning as much as possible, etc), but because this shows the absolute permutation of every number, and then enters that sequence into a hashtable, the hashtable will use its equals() comparison to see that the sequence already exists(such as your last 2 zetas), and reject the duplicate. That way, you'll be left with the answer you want.
The efficiency of the current algorithm: O(|E|^(N)), where |E| is the absolute number of numbers you can have inside of the array at the end of all insertions, and N is the number of insertions(or as you said, # of perplexes). Obviously this isn't the most optimal speed, but it does definitely work.
Hope this helps!

Is O(nk(log(k))) algorithm same as O(n(log(k))) [closed]

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)).

Pseudocode - Largest to smallest integer [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Write a program that will read in five positive integers (one at a time) and print out the largest and smallest number of the five numbers. The program should read the numbers one at a time.
Mind you this is pseudocode and not to be done in any language.
My question is how would I go about setting this up so that the 5 integers save as values so I can display them.
Don't want the answer, just a start.
How would you solve the same problem if you had to only report the largest number? The pseudocode would be something like the following
consider the first number to be largest
for each of the rest of the number
if it is larger then the current largest
assign to largest
How would you do it if there were two?
consider the first number to be largest
if second number is larger then the largest
consider the second number to be largest, first to be 2nd largest
else
consider the first number to be largest, second to be 2nd largest
for each of the rest of the numbers
if it is larger then the largest
consider current largest to be 2nd largest and this number to be largest
else if it is larger then the 2nd largest
consider it to be 2nd largest
But if there are three or more this can get ugly. How do we keep N largest number? Clearly, we need a list of N sorted number. I will leave it to you how to maintain that list, but here's a pseudocode using that approach
populate the top-list with first N numbers from input, ensure the top-list is sorted
for each of the rest of the numbers
if the number is larger then any number in the top-list
insert it at the right place in top list, pushing out the smallest element of the top list
The question now is: is this better than sorting the list and picking up the top N and bottom N elements?
The answer is that "it depends". Can you figure out some circumstances where one approach is better then the other?
As you read the numbers, keep track of the currently largest and smallest numbers, and update the values as the input is coming in. This has the advantage that it works for even long sequences of numbers. I mean something like this:
min = 0
max = 0
while input:
read number from input
if number < min:
min = number
if number > max:
max = number

Resources