Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Let B be the language {0n1n | n >= 0} i.e. 0 and 1 has to have the same length
Let s in B be the string 0p1p
Assume B is regular so s must be divisible to s = xyz where xyiz i>=0 is still in B (Condition 1 of three conditions of pumping lemma).
Consider the case xyiz where i = 2 so xyyz:
Pump y with all 0s
xyyz has more 0s and 1s so it cannot be in B. Therefore, B is not regular.
I am having a hard time understanding that if y is all 0s in xyyz, then # of 0s > # of 1s
Why can't |xyy| = |z| which then it would have the same # of 0s and 1s?
'Pump y with all 0s' isn't terribly clear, but an example of how this works out is as follows:
Pick some value for y: let's say y = '0'.
Pick some value for i: i = 1
Then s = xyz. We will assume this holds true for the moment.
Now, since we assume B is regular, we know that - for any value of i - the string formed by xyiz should also be in B! Let's try xyyz, like you suggest.
...Uh-oh. You see the problem? We have to hold y constant, but doing so means we just made a string that has one more 0 than s, but doesn't have an extra 1 to go with it! We just showed that y can't be 0. Well, darn.
Now consider: is there any value of y for which this won't hold true? Adding 0s to y will only make the issue worse!
This is a very informal walkthrough of the proof, but hopefully it helps understand why it works.
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 10 months ago.
Improve this question
Could anyone please help me in understanding the question for this.
I got this from https://www.geeksforgeeks.org/python-program-for-nth-multiple-of-a-number-in-fibonacci-series/
Given two integers n and k. Find position the n\’th multiple of K in the Fibonacci series.
Examples:
Input : k = 2, n = 3
Output : 9
3'rd multiple of 2 in Fibonacci Series is 34
which appears at position 9.
Input : k = 4, n = 5
Output : 30
5'th multiple of 5 in Fibonacci Series is 832040
which appears at position 30.
You are interested in the numbers that are divisible by k in the fibonacci sequence. Among these numbers, you seek the index of the nth, that is, the index i such that the ith fibonacci number is divisible by k, and such that there are exactly n-1 fibonacci numbers before that one that are also divisible by k.
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 7 months ago.
Improve this question
I am interested in knowing how to calculate a ranking score from ratings of a product. E.g., take the apple appstore. There are two products A and B. Both have same average rating but 100 reviewers have rated A whereas 1000 reviewers have rated B. Intuitively it seems B should be ranked higher than A (it has lower standard error in the mean). Is there an established formula to compare two items and determine which is better based on their ratings?
I write some python code so you can run it easily.
def score(nn):
""" nn = [0, n1, n2, n3, n4, n5] """
if len(nn)==5:
nn = [0, *nn] # add 0
N = sum(nn)
K = 5
kk = [1,2,3,4,5]
z = 1.65 # alpha = 0.1 mean 95% confidence
avg = sum(k*(nn[k]+1)/(N+K) for k in kk)
diff = sum(k**2 * (nn[k]+1)/(N+K) for k in kk) \
- sum(k * (nn[k]+1)/(N+K) for k in kk) ** 2
dev = z * sqrt(diff / (N+K+1))
return avg - dev
You can just call it
score([5, 5, 14, 48, 223]) # 4.517059350728805
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 7 years ago.
Improve this question
http://i.stack.imgur.com/71w9v.png
I have to find how much money a player wins.
This must be done by creating a function called digits Int->Int->Int. For example if the first input is 13758455 and the second input of the function is 13758455 then I should get 1000000 in the compiler.
This is normally an easy task in Java but I wasn't able to find something like a counter to count up as I find more digits in a number.
As there is no state in Haskell, you cannot have counters like you would in imperative languages.
You will often use recursion for this matter. Here is an example:
module Main where
moneyWon :: Int -> Int
moneyWon 8 = 1000000
moneyWon 7 = 100000
moneyWon 6 = 8000
moneyWon 5 = 300
moneyWon 4 = 20
moneyWon 3 = 5
moneyWon 2 = 1
moneyWon _ = 0
digits :: Int -> Int -> Int
digits 0 0 = 0
digits x y = digits dx dy + if mx == my then 1 else 0
where (dx, mx) = divMod x 10
(dy, my) = divMod y 10
main = do
print $ (moneyWon . digits 12345678) 12345668
Notes:
counting corresponding digits and evaluating the prize are two different concerns, that's why I've separated them
the divMod x 10 is a way to get the lowest meaning digit in base 10
in real life, you should not use an integer to hold the digits since it is more a list matter than a number matter (that's why the number starts at 10000000 and not at 0, forcing the user to enter the right number of digits).
Using Int leaves with the task to check the number is valid.
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
Problem Statement
Given N integers, compute their average, correct to three decimal places.
Input Format
The first line contains an integer N.
This is followed by N integers, each on a new line.
Output Format
Display the average of the N integers, rounded off to three decimal places.
Input Constraints
1 <= N <= 500
-10000 <= x <= 10000 (x refers to elements of the list of integers for which the average is to be computed)
Sample Input
4
1
2
9
8
Sample Output
5.000
explaination
The '4' in the first line indicates that there are four integers whose average is to be computed. The average = (1 + 2 + 9 + 8)/4 = 20/4 = 5.000 (correct to three decimal places) Please include the zeroes even if they are redundant (eg. 0.000 instead of 0).
You can use this awk command:
awk 'NR==1{n=$1;next} {s+=$1} END{printf "%.3f\n", s/n}' file
5.000
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I know that an affine cipher substitutes BD with SG. I need to find the encryption formula, in the form y = a x + b, where a and b are coefficients.
From the information above I end up having to equations:
a+b=18 and
3a+b=6
So I am working like this:
a+b=18 and 3a + b = 6-> 3a+18-a=6-> 2a= 6-18 -> 2a=14 (as it is mod 26)
b=18-a
2a=?
So, O want to multiply by the multiplicative inverse of 2 mod 26
I can't find a multiplicative inverse of number 2 with 26 (y = ax + b mod 26)
Can anyone please help me find a and b?
That's because 2 doesn't have a multiplicative inverse mod 26: since 13*2=0, there does not exist K such that K * a = 1. Your modulus must be prime. Try looking up the Chinese Remainder Theorem for more information.
To be more specific, integers mod 26 is not a field (a mathematical set where every element, except 0, has a multiplicative inverse). Any ring in which a * b = 0, for some a!=0 and b!=0, is not a field.
In fact, a field will always have p^n elements, where p is a prime number and n is a positive integer. The simplest fields are just integers mod a prime number, but for prime powers you need to construct a more elaborate system. So, in short, use a different modulus like 29.
Does a = 7 work? 2*7 = 14. Thus, b = 11.
Let's check the 2 equations to see if that works:
7+11 = 18 (check for the first equation).
3*7+11=21+11 = 32 = 6.
What is wrong with the above?
EDIT: Ok, now I see what could go wrong with trying to do a division by 2 in a non-prime modulus as it is similar to a division by 0. You could take ribond's suggestion of using the Chinese Remainder Theorem and split the equations into another pair of pairs:
mod 13: a+b=5, 3a+b=6. (2a = 1 = 14 => a=7. b = 18-7 = 11.)
mod 2: a+b=0. 3a+b=0 (Note this is the same equation and has a pair of possible solutions where a and b are either 0 or 1.)
Thus there is the unique solution for your problem I think.
Other posters are right in that there is no inverse of 2 modulo 26, so you can't solve 2a=14 mod 26 by multiplying through by the inverse of 2. But that doesn't mean that 2a=14 mod 26 isn't solvable.
Consider the general equation cx = d mod n (c=2,d=14,n=26 in your case). Let g = gcd(c,n). The equation cx=d has a solution if an only if g divides d. If g divides d, then there are in fact multiple solutions (g of them). The equation (c/g)x = d/g mod n/g has a unique solution (call it x_0) because c/g is relatively prime to n/g and therefore has an inverse. The solutions to the original equation are x_0, x_0 + n/g, ..., x_0 + (g-1)n/g.
In your case c=2,d=14,n=26, and g=2. g divides d, so first solve the equation (2/2)x = (14/2) mod (26/2) which gives 7. So both 7 and 7+13=20 solve your original equation.
Note that this means you haven't uniquely determined your affine transformation, two possibilities still exist. You need another data point...