Jumping Frog on even number of pads using DrRacket (Scheme) - math

I am trying to solve this question below which first requires finding the math formula then writing code in DrRacket (Scheme dialect of Lisp):
A frog with unlimited jumping power is sitting on the first pad (pad number 0), out of N pads in a row (N is always even). The frog wants to reach the last (Nth) pad. The frog can jump any even number of pads, including N: it’s possible that it will do it in 1 jump (by jumping N pads), in N/2 jumps (by jumping 2 pads at a time) or in any other combination of even jumps. For example, if N were 4, it could jump in one jump: 0th to 4th, or in 2 jumps (from 0th to 2nd and then from 2nd to 4th). The total number of ways of jumping from the 0th to the 4th pad is 2 ways.
What is the math formula for finding the total number of ways the frog can reach the Nth pad?
Using Scheme, write a procedure that, given a number of pads N, returns the total number of ways the frog can reach the Nth pad BY USING RECURSIVE CALLS ONLY. If the number of pad N is not even, the frog can't jump and the procedure returns "x".
I got a solved question which is slightly different and here is the solution for it below, but I am not sure what the math formula is for the question above, though, so would appreciate your help.
A frog with unlimited jumping power is sitting on the first pad (pad number 1), out of N pads in a row. The frog wants to reach the last (Nth) pad. The frog can jump any number of pads, including N: it's possible that it will do it in 1 jump (by jumping N pads), in N jumps (by jumping 1 pad at a time) or in any other combination. For example, if N were 4, it could jump one jump; 1 to 4th, or in 2 jumps; the first of length 1 and then 2 (from 1st to 2nd and then from 2nd to 4th), or the first of length 2 and then 1 (from 1st to 3rd and then from 3rd to 4th), or in 3 jumps (from 1st to 2nd and from 2nd to 3rd and from 3rd to 4th). The total number of ways of jumping from the 1st to the 4th pad is 4 ways. Write a procedure that, given a number of pads N, returns the total number of ways the frog can reach the Nth pad.
Solution:
Total possible jumps = 2 ^ (N-2)
DrRacket Code: (define(pad-jumps n)(power 2 (- n 2))))

Related

Find all the permutations of the set S = {1,2,3,4}. List even and odd permutaions

This is my first post on stack overflow.
I am to submit my maths assignment till 30 April and this the question i have been searching for but i couldn't find any answer anywhere.
I know i can lisst out all possible permutaions which is = 4! = 24
But the question is which of them are even and which of them are odd?
(1,2,3,4), (1,2,4,3), (1,3,2,4) and so on.... Every permutaion will have 3 no. of transposition that means all of them are odd then what's the point of the question? Am I right?
You are not right. The number of transpositions will not always be 3 but will vary.
Your first example (1,2,3,4) needs no transpositions (it is the original order) so it is an even permutation. Your second example (1,2,4,3) can be done with one transposition (swap the 3 and the 4) so it is odd. Your third example (1,3,2,4) can also be done with one transposition (swap the 2 and the 3) so it is odd. And so on.
An example you did not give is (1,3,4,2), which can be done with two transpositions (swap the 2 and the 3, then swap the 2 and the 4) so this is an even transposition. Another final example is (2,3,4,1) which can be done with three transpositions (swap the 1 and 2, then swap 1 and 3, then swap 1 and 4) so this is odd.
No permutation of four elements will require more than three transpositions, but many can be done in fewer. Note that when I say "can be done with one transposition" the permutation can be done with a different number of transpositions, such as with three or five. However, a mathematical theorem states that if a permutation can be done with n transpositions and also with k transpositions, then n and k have the same parity--they are both even or both odd. So an "even permutation" can be done with an even number of transpositions, but we neither know nor care what the exact number is. An "odd permutation" can be done with an odd number of transpositions--one or three or five or ....
Ask if you need help in writing code that determines the parity of a permutation.

Simulate a single n-sided die where the side with the highest number shows up twice as often as all other sides

I need to do this assignment. I just don't know how it works. The question is.
Modify the function roll() from the lecture in a way that it simulates a single n-sided die where the side with the highest number shows up twice as often as all other sides. Functions you may find useful are ?, c(), min(), max(), length(), sort() and rep().
And the function goes.
roll <- function( num = 1:6, rolls = 1) {
dice <- sample(num, size = rolls, replace = TRUE)
return(dice)
}
I'm pretty sure that i have to use the 'prob'-parameters in the sample-Function but i don't know how.
You can do it without the prob argument by thinking about what kind of fairly-weighted (all faces equally probable) die would give the results you want.
sample(1:6, 1) gives you a single sample from an unbiased six-sided die. What you seem to want in this instance is equivalent to a seven-sided die with two sixes. Which would be...
sample(c(1:6,6),1)
That's an equal change of 1 to 5, and double the chance of a 6.
> table(sample(c(1:6,6),7000,replace=TRUE))
1 2 3 4 5 6
972 1018 1016 980 1018 1996
Its not clear to me whether "the highest number shows up twice as often as all other sides" means "all the other sides put together". In which case you want to sample from a 10-sided die with 1 to 5 plus 5 sixes:
sample(c(1:5, rep(6,5)),1)
That's an equal chance of either getting 1 to 5 OR 6.
> table(sample(c(1:5, rep(6,5)),10000,replace=TRUE))
1 2 3 4 5 6
1012 961 943 1018 1026 5040
Generalise to N and write your function.
You are right, the prob-Parameter is useful here (eventhough you could do without).
Here are the steps you have to complete:
Find out which of the entries in num is largest (dont assume that it is the last)
You need the index (="position") of that entry.
Calculate which probability each entry except the largest one would have. Example: If n=6 then each prob is 1/7 with the exception of the last which has 2/7.
Make a vector containing these probabilities in the right positions. You already know the position of the largest, so you would put the doubled prob in that position.
Give the prob to sample().
Test! Run it many times to find out if the largest is really approx. double as often.

How to calculate the expected cost?

I am not good at probability and I know it's not a coding problem directly. But I wish you would help me with this. While I was solving a computation problem I found this difficulty:
Problem definition:
The Little Elephant from the Zoo of Lviv is going to the Birthday
Party of the Big Hippo tomorrow. Now he wants to prepare a gift for
the Big Hippo. He has N balloons, numbered from 1 to N. The i-th
balloon has the color Ci and it costs Pi dollars. The gift for the Big
Hippo will be any subset (chosen randomly, possibly empty) of the
balloons such that the number of different colors in that subset is at
least M. Help Little Elephant to find the expected cost of the gift.
Input
The first line of the input contains a single integer T - the number
of test cases. T test cases follow. The first line of each test case
contains a pair of integers N and M. The next N lines contain N pairs
of integers Ci and Pi, one pair per line.
Output
In T lines print T real numbers - the answers for the corresponding test cases. Your answer will considered correct if it has at most 10^-6 absolute or relative error.
Example
Input:
2
2 2
1 4
2 7
2 1
1 4
2 7
Output:
11.000000000
7.333333333
So, Here I don't understand why the expected cost of the gift for the second case is 7.333333333, because the expected cost equals Summation[xP(x)] and according to this formula it should be 33/2?
Yes, it is a codechef question. But, I am not asking for the solution or the algorithm( because if I take the algo from other than it would not increase my coding potentiality). I just don't understand their example. And hence, I am not being able to start thinking about the algo.
Please help. Thanks in advance!
There are three possible choices, 1, 2, 1+2, with costs 4, 7 and 11. Each is equally likely, so the expected cost is (4 + 7 + 11) / 3 = 22 / 3 = 7.33333.

Right shifting a carry save number

Carry save arithmetic uses twice the number of bits, one word to hold the "virtual sum", one to hold the "virtual carry" to avoid propagating the carry which is the limiting factor in hardware speed.
I have a system that requires dividing these numbers by powers of two, but simply right shifting both numbers does not work in all cases eg. two 16 bit carry save numbers, which you add to produce 4000, C001 is the Virtual Sum, 7FFF is the virtual carry.
C001 + 7FFF = 4000 (discard overflow bits)
but after right shift
6000 + 3FFF = 9FFF (when it should be 2000)
In short: How do you divide a carry save number by a power of two? (While keeping it a carry save number)
First, right shift by 1 effectively does deleting by 2 with forgetting a remainder. But the remainder could be needed for having the exact result. For instance, change your initial example with adding C000 to 8000, or C002 to 7FFE. Both give the same sum but, sum of shifted values is A000 instead of your 9FFF, and this is definitely more correct. So, you can do such shifting only if sum of LSBs could be lost. In your case with 2 summands and 1 bit shift, this means no more than 1 summand could have 1 in its LSB.
Second, consider this is fixed and you've got A000. A simple ideal math says (a+b)/2 == a/2 + b/2. For your case, the carry bit you initially ignored weighed 0x10000, but after shifting by 1, it weighs 0x8000. That is exactly how A000 differs from your expected 2000. So, if you are sure in other aspects of your method, finish it with logical AND with ~0x8000 == 0x7FFF.
There is a technique to correct the representation such that it is shiftable. This originates from a paper "Carry-save architectures for high-speed digital signal processing" by Tobias Noll. You can compute new sign-bits of the carry and sum vectors as
c' = c_out
s' = s xor c xor c_out
where s and c are the original sign-bits and c_out is the discarded carry-bit from the carry-save addition.

Maths Question: number of different permutations

This is more of a maths question than programming but I figure a lot of people here are pretty good at maths! :)
My question is: Given a 9 x 9 grid (81 cells) that must contain the numbers 1 to 9 each exactly 9 times, how many different grids can be produced. The order of the numbers doesn't matter, for example the first row could contain nine 1's etc. This is related to Sudoku and we know the number of valid Sudoku grids is 6.67×10^21, so since my problem isn't constrained like Sudoku by having to have each of the 9 numbers in each row, column and box then the answer should be greater than 6.67×10^21.
My first thought was that the answer is 81! however on further reflection this assumes that the 81 numbers possible for each cell are different, distinct number. They are not, there are 81 possible numbers for each cell but only 9 possible different numbers.
My next thought was then that each of the cells in the first row can be any number between 1 and 9. If by chance the first row happened to be all the same number, say all 1s, then each cell in the second row could only have 8 possibilites, 2-9. If this continued down until the last row then number of different permutations could be calculated by 9^2 * 8^2 * 7^2 ..... * 1^2. However this doesn't work if each row doesn't contain 9 of the same number.
It's been quite a while since I studied this stuff and I can't think of a way to work it out, I'd appreciate any help anyone can offer.
Imagine taking 81 blank slips of paper and writing a number from 1 to 9 on each slip (nine of each number). Shuffle the deck, and start placing the slips on the 9x9 grid.
You'd be able to create 81! different patterns if you considered each slip to be unique.
But instead you want to consider all the 1's to be equivalent.
For any particular configuration, how many times will that configuration be repeated
due to the 1's all being equivalent? The answer is 9!, the number of ways you can permute the nine slips with 1 written on them.
So that cuts the total number of permutations down to 81!/9!. (You divide by the number of indistinguishable permutations. Instead of 9! indistinguishable permutations, imagine there were just 2 indistinguishable permutations. You would divide the count by 2, right? So the rule is, you divide by the number of indistinguishable permutations.)
Ah, but you also want the 2's to be equivalent, and the 3's, and so forth.
By the same reasoning, that cuts down the number of permutations to
81!/(9!)^9
By Stirling's approximation, that is roughly 5.8 * 10^70.
First, let's start with 81 numbers, 1 through 81. The number of permutations for that is 81P81, or 81!. Simple enough.
However, we have nine 1s, which can be arranged in 9! indistinguishable permutations. Same with 2, 3, etc.
So what we have is the total number of board permutations divided by all the indistinguishable permutations of all numbers, or 81! / (9! ** 9).
>>> reduce(operator.mul, range(1,82))/(reduce(operator.mul, range(1, 10))**9)
53130688706387569792052442448845648519471103327391407016237760000000000L

Resources