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 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesnβt differentiate itself from another question.
Improve this question
This is the algorithm:
for i β 1 to n by 1 do
for j β 1 to i by 1 do
for k β 1 to j by 1 do
x = x + 1
end
end
end
The number of times the inner loop iterate, depends on the outer loops... so how can the time complexity be derived?
This algorithm increases π₯ with the πth tetrahedral number. This is because the loops can be seen as sums over a range:
The outer loop: βππ= 1
The middle loop: βππ= 1
The inner loop: βππ= 1
The inner statement: runs in constant time, so that counts as 1 operation.
This means the inner statement runs this many times:
βππ= 1 ( βππ= 1 ( βππ= 1
1 ) )
The inner sum really is π, so we can write:
βππ= 1 ( βππ= 1
π )
We recognise this expression as the tetrahedral number (see formula at above link to Wikipedia)
This double sum is π(π + 1)(π + 2)/6 = ΞΈ(πΒ³)
Another way to see this, is that the loops lead to combinations for π, π, and π where they appear in non-decreasing order. So the number of times x = x + 1 is executed, corresponds to the number of ways we can select a multiset of three values from the range 1..π, allowing duplicates. This number is "π multichoose 3", which leads to the same formula.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Given an integer n between 0 and 10,0000,0000, count the number of integers smaller than n which contain the digits [2,0,1,8] in order.
So e.g. the number 9,230,414,587 should be counted, because removing the digits [9,3,4,4,5,7] leaves us with [2,0,1,8].
Example input and output:
n = 2018 -> count = 1
n = 20182018 -> count = 92237
My general thought is that: the maximum length of n is 10 and the worst situation is that we have to insert 6 digits into [2,0,1,8] and remove the duplicates and the numbers greater than n.
I don't see any own attempts to solve, so I'll give only clue:
You have 9-digits number (small numbers might be represented as 000002018) containing digit sequence 2,0,1,8.
Name them 'good' ones.
Let denote digit places from 1 to 9 right to left:
number 532705183
digits 5 3 2 7 0 5 1 8 3
index 9 8 7 6 5 4 3 2 1
The most left '2' digit can occupy places from 4 to 9. How many good numbers contain the first 2 at k-th place? Let make function F2(l, k) for quantity of good numbers where 2 refers to digit 2, l is number length, k is place for the most left digit.
. . . . 2 . . . .
^
|
left part k right part should contain 0 1 8 sequence
without 2's
F2(9, k) = 9^(9-k) * Sum(F0(k-1, j) for j=1..k-1)
Overall quantity of good numbers is sum of F2(9, k) for all possible k.
GoodCount = Sum(F2(9, k) for k=4..9)
Explanation:
There are 9-k places at the left. We can put any digit but 2 there, so there are 9^(9-k) possible left parts.
Now we can place 0 at the right part and count possible variants for 018 subsequences. F0(...) will of course depend on F1(...) and F1 will depend on F8(...) for shorter numbers.
So fill tables for values for F8, F0, F1 step-by-step and finally calculate result for digit 2.
Hand-made example for 4-digit numbers containing subsequence 1 8 and k = position of the first '1':
k=2: there are 81 numbers of kind xx18
k=3: there are numbers of kind x1x8 and x18x
there are 9 subnumbers like x8, 10 subnumbers 8x, so (10+9)*9=171
k=4: there are numbers of kind
1xx8 (9*9=81 such numbers),
1x8x (9*10=90 numbers),
18xx (100 numbers),
so 81+90+100=271
Overall: 81+171+271=523
This is actually a relatively small problem set. If the numbers were much bigger, I'd opt to use optimised techniques to just generate all numbers that meet your criteria (those containing the digits in that order) rather than generating all possible numbers and checking each to ensure it meets the criteria.
However, the brute force method does your 20182018 variant in about ten seconds and the full 1,000,000,000 range in a little under eight minutes.
So, unless you need it faster than that, you may find the brute-force method more than adequate:
import re
num = 1000000000 # or 20182018 or something else.
lookfor = re.compile("2.*0.*1.*8")
count = 0
for i in range(num + 1):
if lookfor.search(str(i)) is not None:
count += 1
#print(count, i) # For checking.
print(count)
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 6 years ago.
Improve this question
Provide a context-free grammar that generates the following language over
Ξ£ = {0,1}: {w = 0*1* : |w| is odd}
My Solution:
S->AB|0|1
A->0A|^
B->1B|^
But using this grammar we are able to create an even number of string.
I want grammar that produces L = {0,1,000,111,001,011,00000,11111,00001,00011....}
An odd number is the sum of an odd number and an even number, so sentences in the language are either an odd number of 0s followed by an even number of 1s, or an even number of 0s followed by an odd number of ones. Moreover, an odd number is an even number plus one; if we make that substitution in the preceding description we get "an even number of 0s followed by either 0 or 1, followed by an even number of 1s". Since every even number is either 0 or two more than an even number, we end up with.
S -> A 0 B | A 1 B
A -> Ξ΅ | A 0 0
B -> Ξ΅ | B 1 1
or
S -> 0 | 1 | 0 0 S | S 1 1
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have run into a wall since I attended a coding contest. I think this problem falls into the category of fair distribution, but I am not sure.
I am attaching the problem here.
I have come out with an empirical formula so which I tested successfully for 3 iterations attached here.
I am unable to come up with a working code that can solve this programming problem. Any help is appreciated.
Throughout the solution, I use A for hotel and B for house
Let us define 3 types of arrangements:
Arrangement ending with A||B, that is hotel on left and house on right.
Arrangement ending with B||A, that is house on left and hotel on right.
Arrangement ending with B||B, that is house on both sides.
Let us denote the respective arrays:
Array one[i] denotes number of arrangements of type 1 having street length i
Array two[i] denotes number of arrangements of type 2 having street length i
Array three[i] denotes number of arrangements of type 3 having street length i
Any arrangement of type 1, can be extended when the street length increases by 1 as follows:
B||A
A||B [A Type 2 arrangement]
B||B
A||B [A Type 3 arrangement]
Any arrangement of type 2, can be extended when the street length increases by 1 as follows:
A||B
B||A [A Type 1 arrangement]
B||B
B||A [A Type 3 arrangement]
Any arrangement of type 3, can be extended when the street length increases by 1 as follows:
A||B
B||B [A Type 1 arrangement]
B||A
B||B [A Type 2 arrangement]
B||B
B||B [A Type 3 arrangement]
When street length is 1:
one[1] = two[1] = three[1] = 1 (As explained in the sample test case)
When street length is i:
one[i] = two[i-1] + three[i-1]
two[i] = one[i-1] + three[i-1]
three[i] = one[i-1] + two[i-1] + three[i-1]
For a street length of size n, your answer is (one[n] + two[n] + three[n]) % 10^9+7
As n can be very huge, you need to come up with a general formula for the value of (one[n] + two[n] + three[n]) % 10^9+7
For n=1: result = 3
For n=2: result = 7
For n=3: result = 17
For n=4: result = 41
Verify the above results yourself!!!
Edit: You can use Matrix exponentiation to find out the result for different values of n in O(logn) time. Have a look here for more details!!!
You can use a matrix to represent the recurrence relation.
(one[n+1]) = ( 0 1 1 ) (one[n])
(two[n+1]) ( 1 0 1 ) (two[n])
(three[n+1]) ( 1 1 1 ) (three[n])
With this representation, it's feasible to compute values for large n, by matrix exponentation (modulo 10^9+7), using exponentation by squaring. That'll give you the result in O(log n) time.
(one[n]) = ( 0 1 1 )^(n-1) (1)
(two[n]) ( 1 0 1 ) (1)
(three[n]) ( 1 1 1 ) (1)
Happy Coding....
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I see a lot of people saying that the sum of all the multiples of 3 and 5 up to 1000 being one sum and others saying another. Some include the multiples of 15, some don't. So, What is it in reality?
(1..1000).to_a.each do |i|
num = []
if i % 3 == 0
num << i
p num
elsif i % 5 == 0
num << i
p num
end
end
I know there is a better way to code this but when i run this code I don't get repeats. meaning no two 15s
So why would we have to subtract it?
just wondering.
Building upon the original problem statement from project euler which is less ambiguous:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
This means "multiples of either 3 or 5", which naturally includes multiples of 15 one or the other way around. However, your code will not work, because
the num variable is reset to an empty array on every iteration. You should instead initialize the variable outside of the loop
No need to store the numbers in an array, we can directly compute the sum.
1..1000 will iterate up to the number 1000 whereas the problem clearly wants us to find "all the multiples of 3 or 5 below 1000". You can achieve that with an exclusive range 1...1000 (three dots) or by writing 1..999. This should be the primary reason why you get the wrong result.
to_a is not necessary.
you can combine the conditionals with ||, so you dont have to write the identical inner code twice.
All of this leads to
sum = 0
(1...1000).each do |i|
if i % 3 == 0 || i % 5 == 0
sum += i
end
end
For a more functional-ish approach you could rewrite this as follows:
(1...1000).select {|x| x % 3 == 0 || x % 5 == 0 }.inject(:+)
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 7 years ago.
Improve this question
We have this matrix of 4x4:
a b c d
e f g h
1 2 3 4
5 6 7 8
By transposing the matrix we get:
a e 1 5
b f 2 6
c g 3 7
d h 4 8
My question is:
What matrix do we get by "transposing column 2 with row 4?"
I need to understand the operation in itself, what does it imply/mean? I never thought of "transposing a column with a line".
AFAIK, It means you are to swap column 2 and row 4, instead of column 1 with row 1 and column2 with row 2 etc.
The code is basically the same as a full transposition, except you only have one column/row
Matrix transposition is a mathematical operation in which a matrix's rows become its columns. From a mathematical perspective, there's no real benefit to transposing only one row in a M x N matrix, but the code to transpose one row is not much different than transposing an entire matrix.
The matrix you get after the transposition would be:
a b 1 d
e f 2 h
c g 3 7
5 6 4 8