I'm trying to implement movement through four points, while recording which points I visit. Think of it as a square. I can move from corner to corner or diagonally.
If you 'unwrap' the square you get a straight line with four points, which can be thought of as 1-2-3-4- where after 4 it goes back to 1. So if I'm at point 2 I can move to 1 and 3 directly or 4 diagonally. I'd implement that as 2-1 / 2+1 for corner-to-corner or 2+/-2 for diagonally. The problem occurs when I'm at 2 and will try to subtract 2 where I'll end up outside of the list.
The thought I've had is that if I could somehow translate my "out of bounds" numbers to in bounds this would be solved. One solution is hard coding that:
0=4
-1=3
5=1
6=2
but I'm pretty sure there is a better way to do this, however I can't seem to find it.
It seems to me all you want is modular arithmetic (bless the lord for math)
magicFun <- function (x) x %% 4
Here is a simple test run
> magicFun(0:6)
[1] 0 1 2 3 0 1 2
Addendum
It's more about math but the reason it works for negatives is that in Z/nZ ("the world where n is equal to 0") n is "identified" to 0.
This means you can add n as many times as you wish to a given number without changing it's "value".
Also, by convention the numbers in Z/nZ are listed as {0, 1, ..., n-1}.
So suppose n = 4 and x = -6, by the above x = x + 2*4 = 2.
Consider
> data.frame(n=runif(6),m=1:6)
n m
1 0.44000000 1
2 0.12102262 2
3 0.95483015 3
4 0.35628753 4
5 0.55000000 5
6 0.50189420 6
where you want to form the least number of sets having decimal numbers where the sum of numbers is less than 1.
Example trial to find the partions, not necessarily optimal way to find the partitions (particularly with bigger sets)
For example, a partition is a set of number 3 because it is less than one i.e. 0.95483015<1. Then other partition is a set of 5 and 1 because 0.55+0.44<1. And rest numbers go to third partitions such that
partition: 3
partition: 5,1
partition: 2,4,6
now I have a big list of numbers like that which I need to make into least number of partitions or least number of sets having decimal numbers.
Does there exist some R package to find partitions with some optimal criteria like the least number of partitions with some condition?
I have a problem where I would like to replace 2 numbers in a set upto "k"-times such that each time they are switched, I get the largest possible permutation and print this after k-swaps. For example with k=2, for the set (1,4,2,5,3,3) in 1 step I would swap (1,5) to create (5,4,2,1,3,3). In step 2 I would swap (2,3) to create (5,4,3,1,3,2). If after the n < k(th) point we already have the largest permutation e.g. (5,4,3,3,2,1) then we stop.
So far this is what I have:
x<-y<-c(1,4,2,5,3,3)
sx<-sort(x,decreasing=TRUE)
if(k>=n){cat(sx)} else{ ### If we have more operations than numbers?
i<-0; k<-2
m<-max(y)
while(i<k){
if(all(sort(x,decreasing=TRUE)==y)){break}
i<-i+1
a<-max(which(y==m))
while(length(which(y[c(1:a)]<m))==0){
m<-m-1
a<-which(y==m) ### Location of the largest number
if(length(a)==0){
a<-1
next
}
a<-max(a)
}
y[c(min(which(y[c(1:a)]<m)),a)]<-y[c(a,min(which(y[c(1:a)]<m)))]
}
cat(y)
}
5 4 2 1 3 3
Essentially the code finds the max of the current set. Then finds the right most occurrence of this max number. Then finds the left most number lower than the max number. Then switches them. This continues until we have performed k steps OR we have the largest permutation before k. Then prints it.
This code works but takes too long if there are more than 10^4 digits for large k. Is there a way to reduce complexity to O(n) in R?
Here's an O(n log n)-time algorithm (O(n) is impossible with comparisons only, though maybe you know something about the input numbers).
For each subarray of the permutation having (zero-indexed) bounds [i 2^j, (i+1) 2^j) for some integers i, j, store the minimum and the maximum value in the subarray (i.e., set up a segment tree structure).
To find the maximum in an interval in time O(log n), decompose the interval into O(log n) elementary intervals and return the maximum of the maximums.
To find the rightmost maximum in an elementary interval in time O(log n), repeatedly descend to the right child if its maximum is equal to the target, else descend to the left child.
To find the leftmost number lower than a target number in an interval in time O(log n), decompose the interval into O(log n) elementary intervals and find the leftmost elementary interval whose minimum is lower than the target. From this interval, repeatedly descend to the left child if its minimum is lower than the target, else the descend to the right child.
Following your example:
1,5
4,5 1,2 3,3
5 4 2 1 3 3
We just moved 5, and 4 is in the right place, so we want the maximum on the rest of the array. The elementary intervals are 2 1 and 3 3. The maximum is 3. We examine 3 3 and discover that the maximum is in the right child, all the way at the end.
Now we seek the leftmost number less than 3. The elementary intervals are 2 1 and 3 3. We see that 2 1 has a minimum less than 3, so we examine it. So does the left child of 2 1, so 2 is what we swap 3 with.
*1,5*
4,5 *1,2* *3,3*
5 4 3 1 3 2
The aggregates in *asterisks* may need updating (ancestors of the swapped values). There are O(log n) of these. We work bottom up.
*1,5*
4,5 1,3 *3,3*
5 4 3 1 3 2
1,5
4,5 1,3 *3,3*
5 4 3 1 3 2
1,5
4,5 1,3 2,3
5 4 3 1 3 2
Below is a table which has a recursive relation as current cell value is the sum of the upper and left cell.
I want to find the odd positions for any given row denoted by v(x) as represented in the first column.
Currently, I am maintaining two one arrays which I update with new sum values and literally checking if each positions value is odd or even.
Is there a closed form that exists which would allow me to directly say what are the odd positions available (say, for the 4th row, in which case it should tell me that p1 and p4 are the odd places).
Since it is following a particular pattern I feel very certain that a closed form should exist which would mathematically tell me the positions rather than calculating each value and checking it.
The numbers that you're looking at are the numbers in Pascal's triangle, just rotated ninety degrees. You more typically see it written out like this:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
...
You're cutting Pascal's triangle along diagonal stripes going down the left (or right, depending on your perspective) strips, and the question you're asking is how to find the positions of the odd numbers in each stripe.
There's a mathematical result called Lucas's theorem which is useful for determining whether a given entry in Pascal's triangle is even or odd. The entry in row m, column n of Pascal's triangle is given by (m choose n), and Lucas's theorem says that (m choose n) mod 2 (1 if the number is odd, 0 otherwise) can be found by comparing the bits of m and n. If n has a bit that's set in a position where m doesn't have that bit set, then (m choose n) is even. Otherwise, (m choose n) is odd.
As an example, let's try (5 choose 3). The bits in 5 are 101. The bits in 3 are 011. Since the 2's bit of 3 is set and the 2's bit of 5 is not set, the quantity (5 choose 3) should be even. And since (5 choose 3) = 10, we see that this is indeed the case!
In pseudocode using relational operators, you essentially want the following:
if ((~m & n) != 0) {
// Row m, entry n is even
} else {
// Row m, entry n is odd.
}
I'm trying to produce a set of 480 random integers between 1-9. However there are some constraints:
the sequence cannot contain 2 duplicate digits in a row.
the sequence must include exactly 4 sequences of odd numbers and 4 sequences of even numbers (in any order) within every 80 digit sequence (e.g. 6 4 5 2 4 8 3 4 6 9 1 5 4 6 1).
I have been able to produce a set of random numbers hat allows repeated digits, using:
NumRep <- sample(1:9, 480, replace=T)
but not been able to work out how to allow digits to be repeated over the entire set, but to not allow sequential repeats (e.g. 2 5 3 would be okay, 2 5 5 would not). I have got nowhere with the odd/even constraint.
For context, this is not homework! I am a researcher, and this is part of a psychological experiment that I am creating.
Any help would be greatly appreciated!
First, the problem loses the "random" way of simulating with that conditions. Anyway this code corresponds with the first constraint:
# Build a vector
C<-vector()
# Length of the vector
n<-480
# The first element
C<-sample(1:9,1)
# Complete the rest
for (i in 2:n){
# Take a random number not equal to the previous one
C[i] <- sample(c(1:9)[1:9!=C[i-1]],1)
}
# It is an odd number?
C %% 2 == 0
# How many consecutive odd numbers are in the sequence?
# Build a table with this information
TAB <- rle( C %% 2 == 0)
# Maximum of consecutive odd numbers
max(TAB$lengths[TAB$values==T])
# Maximum of consecutive even numbers
max(TAB$lengths[TAB$values==F])
I don't understand the second constraint, but I hope the final part of the code helps. You should use that information in order to interchange some values.