I wish to write a program in R to generate 1 random number (a positive integer) each starting from 3 digits to 12 digits following these conditions:
There is no order in the consecutive number digits.
Strictly no repetition of digits in a number until the 9th digit number.
0 can be used after the 9 digit number only.
After 10 digits, a digit can be used twice but with no order.
And most importantly:
**The first number will not be the last number of next line and vice versa. **
All I know how to use is the sample command in R:
sample(1:9, size=n, replace=FALSE)
where n is the number of digits I wish to generate. However, I need to write a more generalized function or program which strictly obeys these conditions.
Related
1.I want to generate combinations of characters from a given word with each letter being repeated consecutively utmost 2 times and at least 1.The resultant words are of unequal lengths. For example from
"cat"
to
"cat", "catt", "caat", "caatt", "ccat", "ccatt", "ccaat", "ccaatt"
Required function takes a word of length n and generates 2^n words of unequal length. It is almost similar to binary digits with n length gives 2^n combinations. For example a 3 digit binary number gives
000 001 010 011 100 101 110 111
combinations, where 0=t and 1=tt.
2.And also the same function should restrict the resultant sequence maximum upto 2 consecutive repetition of a character even if the given word has repetitions of letters.For example
"catt"
to
"catt" "ccatt" "caatt" "ccaatt"
I tried something like this
pos=expand.grid(l1=c(1,11),l2=c(2,22),l3=c(3,33))
result=chartr('123','cat',paste0(pos[,1],pos[,2],pos[,3]))
#[1] "cat" "ccat" "caat" "ccaat" "catt" "ccatt" "caatt" "ccaatt"
It gives correct sequence but I am stuck with generalizing it to any given word with different lengths.
Thank you.
Use stdout as per normal...
print("Hello, world!")
x="cat"
l=seq(nchar(x))
n=paste(l,collapse="")
m=split(c(l,paste0(l,l)),rep(l,2))
chartr(n,x,do.call(paste0,expand.grid(m)))
1.Just an addition to the answer given by Onyambu to solve the second part of the question i.e. restrict the output to maximum 2 consecutive repetitions of a character given any number of consecutive repetitions of characters in the input word.
x="catt"
l=seq(nchar(x))
n=paste(l,collapse="")
m=split(c(l,paste0(l,l)),rep(l,2))
o <- chartr(n,x,do.call(paste0,expand.grid(m)))
Below line of code removes the words with more than 2 consecutive repetitive characters
unique(gsub('([[:alpha:]])\\1{2,}', '\\1\\1', o))
#[1] "catt" "ccatt" "caatt" "ccaatt"
2.If you want all the combinations starting from "cat" to "ccaattt" given any number of consecutive repetitions of characters in the input word. Code is
x1="catt"
Below line of code restricts the consecutive repetition of characters to 1.
x2= gsub('([[:alpha:]])\\1+', '\\1', x1)
l=seq(nchar(x2))
n=paste(l,collapse="")
m=split(c(l,paste0(l,l)),rep(l,2))
o <- chartr(n,x,do.call(paste0,expand.grid(m)))
unique(gsub('([[:alpha:]])\\1{2,}', '\\1\\1', o))
#[1] "cat" "ccat" "caat" "ccaat" "catt" "ccatt" "caatt" "ccaatt"
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.
I was wondering if there is any formula or way to find out much maximum bits will be required if two n bits binary number are multiplied.
I searched a lot for this but was unable to find its answer anywhere.
Number of digits in base B required for representing a number N is floor(log_B(N)+1). Logarithm has this nice property that log(X*Y)=log(X)+log(Y), which hints that the number of digits for X*Y is roughly the sum of the number of digits representing X and Y.
It can be simply concluded using examples:
11*11(since 11 is the maximum 2 bit number)=1001(4 bit)
111*111=110001(6 bit)
1111*1111=11100001(8 bit)
11111*11111=1111000001(10 bit)
and so from above we can see your answer is 2*n
The easiest way to think about this is to consider the maximum of the product, which is attained when we use the maximum of the two multiplicands.
If value x is an n-bit number, it is at most 2^n - 1. Think about this, that 2^n requires a one followed by n zeroes.
Thus the largest possible product of two n-bit numbers will be:
(2^n - 1)^2 = 2^(2n) - 2^(n+1) + 1
Now n=1 is something of a special case, since 1*1 = 1 is again a one-bit number. But in general we see that the maximum product is a 2n-bit number, whenever n > 1. E.g. if n=3, the maximum multiplicand is x=7 and the square 49 is a six-bit number.
It's worth noting that the base of the positional system doesn't matter. Whatever formula you come up with for the decimal multiplication will work for the binary multiplication.
Let's apply a bit of deduction and multiply two numbers that have relatively prime numbers of digits: 2 and 3 digits, respectively.
Smallest possible numbers:
10 * 100 = 1000 has 4 digits
Largest possible numbers:
99 * 999 = 98901 has 5 digits
So, for a multiplication of n-digit by m-digit number, we deduce that the upper and lower limits are n+m and n+m-1 digits, respectively. Let's make sure it holds for binary as well:
10 * 100 = 1000 has 4 digits
11 * 111 = 10101 has 5 digits
So, it does hold for binary, and we can expect it to hold for any base.
x has n binary digits means that 2^(n-1) <= x < 2^n, also assume that y has m binary digits. That means:
2^(m+n-2)<=x*y<2^(m+n)
So x*y can have m+n-1 or m+n digits. It easy to construct examples where both cases are possible:
m+n-1: 2*2 has 3 binary digits (m = n = 2)
m+n: 7*7=49=(binary)110001 has 6 binary digits and m = n = 3
I lack the math skills to make this function.
basically, i want to return 2 random prime numbers that when multiplied, yield a number of bits X given as argument.
for example:
if I say my X is 3 then a possible solution would be:
p = 2 and q = 3 becouse 2 * 3 = 6 (110 has 3 bits).
A problem with this statement is that it starts by asking for two "random" prime numbers. Without any explicit statement of the distribution of the required random primes, we are already stuck. (This is the beginning of a classic paradox, where we are asked to generate a "random" integer.)
But suppose that we change the statement to finding any two arbitrary primes, that yield the desired product with a given number of bits x. The answer is trivial.
The set of numbers that have exactly x bits in their binary representation is the half open set of integers [2^(x-1),2^x-1].
Choose an arbitrary prime number that is less than or equal to (2^x-1)/2. Call it p1.
Next, choose a second prime number that lies in the interval (2^(x-1)/p1,(2^x-1)/p1). Call it p2.
It must be true that p1*p2 will be in the desired interval.
For example, given x = 10, so the product must lie in the interval [512,1023], the set of integers with exactly 10 bits. (Note, there are apparently 147 such numbers in that interval, with exactly two prime factors.)
Step 1:
Choose p1 as any prime no larger than 1023/2 = 511.5. I'll pick p1 = 137. Then the second prime factor must be a prime that lies in the interval
[512 1023]/137
ans =
3.7372 7.4672
thus either 5 or 7.
dec2bin(137*[5 7])
ans =
1010101101
1110111111
If you know the number of bits, you can generate a number 2^(x-2) < x < 2^(x-1). Then take the square root and find the closest primes on either side of it. Multiplying them together will, in most cases, get you a number in the correct range. If it's too high, you can take the two primes directly on the lower side of it.
pseudocode:
x = bits
primelist[] = makeprimelist()
rand = randnum between 2^(x-2) and 2^(x-1)
n = findposition(primelist, rand)
do
result = primelist[n]*primelist[n+1]
n--
while result > 2^(x-1)
Note that numbers generated this way will allways have '1' as the highest significant bit, so would be possible to generate a number of x-1 bits and just tack the 1 onto the end.
What is a canonical signed digit (CSD) and how does one convert a binary number to a CSD and a CSD back to a binary number? How do you know if a digit of a CSD should be canonically chosen to be +, -, or 0?
Signed-digit binary uses three symbols in each power-of-two position: -1, 0, 1. The value represented is the sum of the positional coefficients times the corresponding power of 2, just like binary, the difference being that some of the coefficients may be -1. A number can have multiple distinct representations in this system.
Canonical signed digit representation is the same, but subject to the constraint that no two consecutive digits are non-0. It works out that each number has a unique representation in CSD.
See slides 31 onwards in Parhi's Bit Level Arithmetic for more, including a binary to CSD conversion algorithm.
What is canonical signed digit format?
Canonical Signed Digit (CSD) is a type of number representation. The important characteristics of the CSD presentation are:
CSD presentation of a number consists of numbers 0, 1 and -1. [1, 2].
The CSD presentation of a number is unique [2].
The number of nonzero digits is minimal [2].
There cannot be two consecutive non-zero digits [2].
How to convert a number into its CSD presentation?
First, find the binary presentation of the number.
Example 1
Lets take for example a number 287, which is 1 0001 1111 in binary representation. (256 + 16 + 8 + 4 + 2 + 1 = 287)
1 0001 1111
Starting from the right (LSB), if you find more than non-zero elements (1 or -1) in a row, take all of them, plus the next zero. (if there is not zero at the left side of the MSB, create one there). We see that the first part of this number is
01 1111
Add 1 to the number (i.e. change the 0 to 1, and all the 1's to 0's), and force the rightmost digit to be -1.
01 1111 -> 10 000-1
You can check that the number is still the same: 16 + 8 + 4 + 2 + 1 = 31 = 32 + (-1).
Now the number looks like this
1 0010 000-1
Since there are no more consecutive non-zero digits, the conversion is complete. Thus, the CSD presentation for the number 287 is 1 0010 000-1, which is 256 + 31 - 1.
Example 2
How about a little more challenging example. Number 345. In binary, it is
1 0101 1001
Find the first place (starting from righ), where there are more than one non-zero numbers in a row. Take also the next zero. Add one to it, and force the rightmost digit to be -1.
1 0110 -1001
Now we just created another pair of ones, which has to be transformed. Take the 011, and add one to it (get 100), and force the last digit to be -1. (get 10-1). Now the number looks like this
1 10-10 -1001
Do the same thing again. This time, you will have to imagine a zero in the left side of the MSB.
10 -10-10 -1001
You can make sure that this is the right CSD presentation by observing that: 1) There are no consecutive non-zero digits. 2) The sum adds to 325 (512 - 128 - 32 - 8 + 1 = 345).
More formal definitions of this algorithm can be found in [2].
Motivation behind the CSD presentation
CSD might be used in some other applications, too, but this is the digital microelectronics perspective. It is often used in digital multiplication. [1, 2]. Digital multiplication consists of two phases: Computing partial products and summing up the partial product. Let's consider the multiplication of 1010 and 1011:
1010
x 1011
1010
1010
0000
+ 1010
= 1101110
As we can see, the number of non-zero partial products (the 1010's), which are has to be summed up depends on the number of non-zero digits in the multiplier. Thus, the computation time of the sum of the partial products depends on the number of non-zero digits in the multiplier. Therefore the digital multiplication using CSD converted numbers is faster than using conventional digital numbers. The CSD form contains 33% less non-zero digits than the binary presentation (on average). For example, a conventional double precision floating point multiplication might take 100.2 ns, but only 93.2 ns, when using the CSD presentation. [1]
And how about the negative ones, then. Are there actually three states (voltage levels) in the microcircuit? No, the partial products calculated with the negative sign are not summed right away. Instead, you add the 2's complement (i.e. the negative presentation) of these numbers to the final sum.
Sources:
[1] D. Harini Sharma, Addanki
Purna Ramesh: Floating point multiplier using Canonical Signed
Digit
[2] Gustavo A. Ruiz, Mercedes Grand: Efficient canonic signed digit recoding