How to proceed with spoj LGIC? - math

I am trying to solve this problem http://www.spoj.pl/problems/LGIC/. I just can't figure out how is this sequence advancing.
By lagarange's it is too complex to solve for such a great range.
The farthest I could get was with factorials
1! = 1 & a1=2
2! = 2 & a2=4
3! = 6 & a3=11
4! = 24 & a4=36
5! = 120 & a5=147
6! = 720 & a6=778
Please guide me someone..

Perhaps the sequence is: An = n! + 2^n - n.

You can gat the Nth term using
T(n) = n! + pow(2,n) - n

Related

Is it possible to find a few common multiples of a list of numbers, without them having to be integers?

I don't even know if something like this is possible, but:
Let us say we have three numbers:
A = 6
B = 7.5
C = 24
I would like to find a few evenly spaced common multiples of these numbers between 0 and 2.
So the requirement is: one_of_these_numbers / common_multiple = an_integer (or almost an integer with a particular tolerance)
For example, a good result would be [0.1 , 0.5 , 1 , 1.5]
I have no idea if this is possible, because one can not iterate through a range of floats, but is there a smart way to do it?
I am using python, but a solution could be represented in any language of your preference.
Thank you for your help!
While I was writing my question, I actually came up with an idea for the solution.
To find common divisors using code, we have to work with integers.
My solution is to multiply all numbers by a factor = 1, 10, 100, ...
so that we can act as if they are integers, find their integer common divisors, and then redivide them by the factor to get a result.
Better explained in code:
a = 6
b = 7.5
c = 24
# Find a few possible divisors between 0 and 2 so that all numbers are divisible
by div.
# We define a function that finds all divisors in a range of numbers, supposing
all numbers are integers.
def find_common_divisors(numbers, range_start, range_end):
results = []
for i in range(range_start + 1, range_end + 1):
if all([e % i == 0 for e in numbers]):
results.append(i)
return results
def main():
nums = [a, b, c]
range_start = 0
range_end = 2
factor = 1
results = [1]
while factor < 11:
nums_i = [e * factor for e in nums]
range_end_i = range_end * factor
results += [e / factor for e in find_common_divisors(nums_i, range_start, range_end_i)]
factor *= 10
print(sorted(set(results)))
if __name__ == '__main__':
main()
For these particular numbers, I get the output:
[0.1, 0.3, 0.5, 1, 1.5]
If we need more results, we can adjust while factor < 11: to a higher number than 11 like 101.
I am curious to see if I made any mistake in my code.
Happy to hear some feedback.
Thank you!

What should be the Big O of f(n) = n^5 + 2^log(n)?

I came across a problem where I've to select the correct Big O for the function f(n) = n^5 + 2^log(n)...
I tried putting large values and found out that n^5 grows significantly as compare to 2^log(n)... But then someone told me that exponential functions grow significantly as compared to other functions... And I got confused again... To be honest I think 2^log(n) is not an exponential function... But because of my weak logarithmic concepts, I am unable to prove that...
I just want someone to tell me that yes n^5 is larger than 2^log(n) so that I can prove that 2^log(n) is not an exponential function...
Thanks in advance. :)
2^log(n) = (2/e)^log(n) * e^log(n) = a^log(n) * n where a = 2/e < 1 (assuming log is the natural logarithm).
It follows that f(n) = n^5 + 2^log(n) < n^5 + n and therefore f(n) = O(n^5).
[ EDIT ]   In the general case of logarithms of an arbitrary base b, using that 2 = b^log_b(2) it follows that:
2^log_b(n) = (b^log_b(2))^(log_b(n))
= b^(log_b(2)*log_b(n))
= (b^log_b(n))^log_b(2)
= n^log_b(2)
= n^(1/log_2(b))
Therefore f(n) = n^5 + log_b(n) = O( n^5 + n^(1/log_2(b)) ) = O( n^max(5, 1/log_2(b)) ).
In particular, f(n) = O(n^5) for log_2(b) > 1/5 ⇔ b > 2^(1/5), which covers the common log bases of 2, e, 10.
O(2logn)=O(n) - this follows straight from the definition of logarithm.
More formally:
f(n)=2logn
log2f(n)=log2(2logn)=lognlog22=log2n
==>f(n)=n
==> O(2logn)=O(n)
==> O(n5 + 2logn)=O(n5 + n)=O(n5)

What is (sqrt(1+8*long(num)) - 1)/2?

Hi I was looking up solutions to a leetcode problem, I know how to solve the problem but someone else submitted this solution and I don't understand how this works.
The question is how many number of stacks can you form with n number of coins where k-th row has k number of coins. https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3377/
Returning the above formula works, can anyone explain it to me?
The sum of the first N natural numbers (1 + 2 + 3 + ... + N) is known to be equal to N(N+1)2
The game says that if you have 6 coins you have to stack them in this way:
x (1)
x x (2)
x x x (3)
and 6 is equal to 1 + 2 + 3.
If you are given K coins and you know that N(N+1)/2 = K then you know that you can have N rows. Now the question is, given K, how can you find N?
Let's do the math:
N(N+1)/2 = K
N^2 + N = 2*K
N^2 + N -2*K = 0
N = (-1 + sqrt(1 + 8K))/2
N = -1/2 + sqrt(1/4 + 2K)
N = sqrt(2*K + 0.25) - 0.5

Not understanding the recurrence formula of n nodes with a height h in an AVL tree to show h <= 2 log n

I know the formula is: n(h) = n(h-1) + n(h-2) + 1
And I know it can be reduced as:
n(h) = n(h-1) + n(h-2) + 1
>= n(h-2) + n(h-2) + 1
>= 2n(h-2) + 1
>= 2n(h-2)
After this step I don't understand the recurrence that would come here. I was reading a proof online and they did this:
>= 2n(h-2)
>= 2(2n(h-4))
>= 2(2(2n(h-6)))
I'm not understanding that block. Why is each step multiplied by 2 and why is 2 more subtracted each time from the height? I'm having trouble visualizing it or something. Then the rest of the proof shows:
>=(2^i)n(h-2i)
I understand how they got that answer based on the pattern, and I can solve the rest of the proof, but I don't understand how that recursive pattern was chosen. I hope I'm making sense. If anyone can clarify this for me, I would appreciate it very much!
Given that n(h) >= 2n(h-2) for all h, we can apply this very same inequality for h-2 as well. This would give us:
n(h-2) >= 2n(h-2-2)
which is the same as
n(h-2) >= 2n(h-4).
If we now apply it again for h-4 (as we did for h-2) we get
n(h-4) >= 2n(h-4-2) = 2n(h-6).
Now we can replace these two last inequalities into the first one:
n(h) >= 2n(h-2) >= 2(2n(h-4)) >= 2(2(2n(h-6)))
and so on...

Can someone explain Mathematical Induction (to prove a recursive method)

Can someone explain mathematical induction to prove a recursive method? I am a freshmen computer science student and I have not yet taken Calculus (I have had up through Trig). I kind of understand it but I have trouble when asked to write out an induction proof for a recursive method.
Here is a explanation by example:
Let's say you have the following formula that you want to prove:
sum(i | i <- [1, n]) = n * (n + 1) / 2
This formula provides a closed form for the sum of all integers between 1 and n.
We will start by proving the formula for the simple base case of n = 1. In this case, both sides of the formula reduce to 1. This in turn means that the formula holds for n = 1.
Next, we will prove that if the formula holds for a value n, then it holds for the next value of n (or n + 1). In other words, if the following is true:
sum(i | i <- [1, n]) = n * (n + 1) / 2
Then the following is also true:
sum(i | i <- [1, n + 1]) = (n + 1) * (n + 2) / 2
To do so, let's start with the first side of the last formula:
s1 = sum(i | i <- [1, n + 1]) = sum(i | i <- [1, n]) + (n + 1)
That is, the sum of all integers between 1 and n + 1 is equal to the sum of integers between 1 and n, plus the last term n + 1.
Since we are basing this proof on the condition that the formula holds for n, we can write:
s1 = n * (n + 1) / 2 + (n + 1) = (n + 1) * (n + 2) / 2 = s2
As you can see, we have arrived at the second side of the formula we are trying to prove, which means that the formula does indeed hold.
This finishes the inductive proof, but what does it actually mean?
The formula is correct for n = 0.
If the formula is correct for n, then it is correct for n + 1.
From 1 and 2, we can say: if the formula is correct for n = 0, then it is correct for 0 + 1 = 1. Since we proved the case of n = 0, then the case of n = 1 is indeed correct.
We can repeat this above process again. The case of n = 1 is correct, then the case of n = 2 is correct. This reasoning can go ad infinitum; the formula is correct for all integer values of n >= 1.
induction != Calc!!!
I can get N guys drunk with 10*N beers.
Base Case: 1 guy
I can get one guy drunk with 10 beers
Inductive step, given p(n) prove p(n + 1)
I can get i guys drunk with 10 * i beers, if I add another guy, I can get him drunk with 10 more beers. Therefore, I can get i + 1 guys drunk with 10 * (i + 1) beers.
p(1) -> p(i + 1) -> p(i + 2) ... p(inf)
Discrete Math is easy!
First, you need a base case. Then you need an inductive step that holds true for some step n. In your inductive step, you will need an inductive hypothesis. That hypothesis is the assumption that you needed to have made. Finally, use that assumption to prove step n+1

Resources