In R, I need to create a vector b = (1, 1+pi, 1+2pi, 1+3pi,...,1+19pi). I am unsure how to do this. I keep trying to use the seq command (i.e. seq(1, 1+npi n = 1:19) and that's totally wrong!), but don't know the proper syntax to make it work, thus it never does.
Any help would be appreciated.
R needs the multiplication operator.
b <- 1+ seq(0,19)*pi
Or slightly faster in situations where speed might matter:
b <- 1+ seq.int(0,19)*pi
You could use the equivalent:
b <- 1+ 0:19*pi
Because the ":" operator has very high precedence ( see ?Syntax), it's reasonable safe. Just be careful that you understand precedence when you use a minus or plus sign where it might be parse as a binary operator (remembering that spaces are ignored and that unary-minus has higher precedence than the single-colon, but binary minus or plus has a lower precedence :
> 1: 5+5
[1] 6 7 8 9 10
You should use simply 0:19 * pi + 1. Using seq is not so nice: seq(1, 1 + 19 * pi, by = pi) or seq(1, 1 + 19 * pi, length = 20).
Related
I write a code in R to reverse a number. But I got inf as output.
digit<-512
rev_num<-0
while(digit>0){
rev_num=rev_num*10 + digit %% 10
digit=digit / 10
}
print(paste(rev_num))
Can anyone tell me the error in this code?
A quick fix to your approach would add floor for digit when dividing by 10.
digit<-512
rev_num<-0
while(digit>0){
rev_num=rev_num*10 + digit %% 10
digit= floor(digit / 10)
}
rev_num
#[1] 215
There is also stri_reverse function in stringi
stringi::stri_reverse(512)
#[1] "215"
You need digit = digit %/% 10 instead of / where %/% is to be used for integer division. And you need integer division because using / gives residual decimal places and your while loop do not stop until digit reaches the minimum number that can be represented by your machine while your rev_num keep growing by a multiple of 10 in
each iteration, reaching Inf.
Fix to your code (digit here is not an integer, so when you divide it by 10, it goes 51.2, then 5.12 and so on, which is why you got INF as output):
digit<-512
rev_num<-0
while(digit>0){
rev_num=rev_num*10 + digit %% 10
digit=as.integer(digit / 10)
}
print(paste(rev_num))
Another approach to reversing a number:
z <- 4321
as.numeric(paste(rev(strsplit(as.character(z),"")[[1]]),collapse=""))
First, you convert the number into a string
Then, You can use stri_reverse() function form stringi
stri_reverse(<String value to reverse>)
Then convert the string into Number.
Maybe you can try the base R code below, using toString + utf8ToInt + intToUtf8:
digit<-512
rev_num <- as.numeric(intToUtf8(rev(utf8ToInt(toString(digit)))))
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 6 years ago.
Improve this question
i know how to convert from base b to decimal but Im not understand what is base-b exactly. I know we multiply the base to the numbers if we gonna convert to bas 10 then multiply with then is base-b(base 5)?
In order to follow this, we should understand the difference between a number and its representation. Let's start with the (natural) numbers. There are two special numbers: zero and one. Zero is the neutral element of addition (i.e. you can add zero to anything without changing it) and one is the neutral element of multiplication. Every other number can be induced by these two numbers. Start with zero. Then, subsequently add one.
A common representation for numbers is the decimal system. However, this is purely arbitrary and any other system could be used as well. There is nothing intrinsic in the number twelve that would require us to write it as 12. The nice thing is that all arithmetic rules are defined on the numbers themselves, not on their representations. Five plus six will always be eleven. No matter how you represent them. You may have already noticed that I use number words when I talk about the numbers and any other representation if I talk about the representation.
Ok, so we have our numbers. Now we need a way to represent them. Imagine we have three symbols a, b, and c. We could just assign the first three numbers to them
a (zero)
b (one)
c (two)
But then we are out of symbols. As you know, the positional numeral systems solve this by introducing another position. Then, just continue as before. Assign the next few numbers in order
ba (three)
bb (four)
bc (five)
ca (six)
cb (seven)
cc (eight)
You might want to continue with a third position:
baa (nine)
bab (ten)
bac (eleven)
...
The base of this system is three (or ba) because we have three symbols. We can observe that the digits in the second position stand for an addition of a multiple of three (b. stands for three + ., c. stands for two times three + . ...) Expressed in base ba, this is: b. = b * ba + ., c. = c * ba + .. This continues to all positions and you can generalize that a number formed of digits dn ... d1 d0 can be expressed by the well-known formula:
n = Sum(i) di * base^i
The intuition behind this formula is that there will be base numbers with one digit, base^2 numbers with two digits and so on. And the di * base^i term skips the first few of them (as many such that the first digit matches, then the second and so on).
We can check this at the example of bac which should be eleven:
n = b * ba^c + a * ba^a + c * ba^a
= one * three^two + zero * three^one + two * three^zero
= nine + zero + two
= eleven
= bac
Remember that the arithmetic rules apply to the numbers and not to the representations? So since we know the definition of our number (second line in the above formula), we can use any other number representation. For example, the decimal one:
n = one * three^two + zero * three^one + two * three^zero
= 1 * 3^2 + 0*3^1 + 2*3^0
= 9 + 0 + 2
= 11 (decimal)
But we could also use another base, e.g. base-8:
n = one * three^two + zero * three^one + two * three^zero
= 1 * 3^2 + 0*3^1 + 2*3^0
= 11 + 0 + 2
= 13 (octal)
So basically, these systems arise naturally by assigning digit sequences systematically to subsequent numbers. The conversion is so simple because the positional equation applies to the numbers, not to the representations.
I hope this answer was not too abstract and helped you.
so I have a loop that finds the position in the matrix where there is the largest difference in consecutive elements. For example, if thematrix[8] and thematrix[9] have the largest difference between any two consecutive elements, the number given should be 8.
I made the loop in a way that it will ignore comparisons where one of the elements is NaN (because I have some of those in my data). The loop I made looks like this.
thenumber = 0 #will store the difference
for (i in 1:nrow(thematrix) - 1) {
if (!is.na(thematrix[i]) & !is.na(thematrix[i + 1])) {
if (abs(thematrix[i] - thematrix[i + 1]) > thenumber) {
thenumber = i
}
}
}
This looks like it should work but whenever I run it
Error in if (!is.na(thematrix[i]) & !is.na(thematrix[i + 1])) { :
argument is of length zero
I tried this thing but with a random number in the brackets instead of i and it works. For some reason it only doesn't work when I use the i specified in the beginning of the for-loop. It doesn't recognize that i represents a number. Why doesn't R recognize i?
Also, if there's a better way to do this task I'd appreciate it greatly if you could explain it to me
You are pretty close but when you call i in 1:nrow(thematrix) - 1 R evaluates this to make i = 0 which is what causes this issue. I would suggest either calling i in 1:nrow(thematrix) or i in 2:nrow(thematrix) - 1 to start your loop at i = 1. I think your approach is generally pretty intuitive but one suggestion would be to frequently use the print() function to evaluate how i changes over the course of your function.
The issue is that the : operator has higher precedence than -; you just need to use parentheses around (nrow(thematrix)-1). For example,
thematrix <- matrix(1:10, nrow = 5)
##
wrong <- 1:nrow(thematrix) - 1
right <- 1:(nrow(thematrix) - 1)
##
R> wrong
#[1] 0 1 2 3 4
R> right
#[1] 1 2 3 4
Where the error message is coming from trying to access the zero-th element of thematrix:
R> thematrix[0]
integer(0)
The other two answers address your question directly, but I must say this is about the worst possible way to solve this problem in R.
set.seed(1) # for reproducible example
x <- sample(1:10,10) # numbers 1:10 in random order
x
# [1] 3 4 5 7 2 8 9 6 10 1
which.max(abs(diff(x)))
# [1] 9
The diff(...) function calculates sequential differences, and which.max(...) identifies the element number of the maximum value in a vector.
I would like to solve following equation:
x^2-bitxor(2,x)=0
Is it possible ?
bitxor(2,x) wil either add 2 or subtract 2 to/from x, so you have to solve both:
x^2 - x + 2 = 0
and:
x^2 - x - 2 = 0
and then test to see if the any of the solutions work in the original expression.
In the first case the roots are complex, in the second case the roots are -1 and +2.
Substituting back into the original equation:
x = -1 => (-1^2) - bitxor(2, -1) = 1 - (-3) = 4
x = 2 => (2^2) - bitxor(2, 2) = 4 - 0 = 4
So apparently there is no real integer solution.
There are solutions in modular arithmetic, which is precisely the arithmetic that most computers use for unsigned integers. For example, consider the case of modulo 256 arithmetic. In C and C++, this is the arithmetic of unsigned char. Here we have two solutions, 91 and 166. (See http://www.wolframalpha.com/input/?i=x%5E2+mod+256+%3D+x+-+2%2C+x+%3E+0%2C+x%3C+256 ).
In fact, for any modulo 2^N system, there will be two solutions that sum to 1 (modulo 2^N).
I want to make a short URL service for 2 million assets but I want to use the shortest number of possible characters.
What is the math equation that I would need to use to figure it out? I know it has something to do with factorials, right?
It's not a factorial problem, but an exponential one.
If x is the number of possible characters, you need to solve the following equation for y:
x^y = 2000000
If you want to use all numbers and case-sensitive alpha [0-9A-Za-z], you have 62 possible values. This means you need to solve:
62^y = 2000000
y*log(62) = log(2000000)
y = log(2000000) / log(62)
y = 3.5154313828...
Of course, you can't have 3.5 characters in your URL, so you would need 4. If you want to change the character set you are using for your URL's, simply resolve the problem above using the number of values in your set.
Note Solving this equation assumes fixed-length URL's. For variable-length URL's, see Rob's answer.
#jheddings is close, and got the right answer, but the math was not quite correct. Don't forget you are not limited to all the permutations of characters of a specific length. You can also leverage URLs of length 1 through y characters. Therefore we want the closed value of this sum:
x + x^2 + x^3 + ... + x^y = 2000000
Fortunately, there is a closed form for that sum:
x + x^2 + x^3 + ... + x^y = x*(x^y - 1)/(x-1) = 2000000
x is the number of possible characters in our range. For simplicity sake, let's assume it only includes lowercase, uppercase, and numbers (26+26+10 = 62.)
Then we get the following equation:
2000000 = (62^(y+1) - 62)/(62-1)
2000000 = (62^(y+1) - 62)/(61)
2000000 * 61 = 62^(y+1) - 62
122000000 = 62^(y+1) - 62
122000000 + 62 = 62^(y+1)
122000062 = 62^(y+1)
log(122000062) = (y+1)
log(122000062) / log(62) = y+1
4.511492 = y+1
3.511492 = y
And, as you said, 3.5 characters is impossible so 4 are required. Admittedly the difference doesn't matter in this case. However, in certain scenarios (especially when dealing with base 2) it is very important.
Number of possible short URLs = (Number of possible different characters in ID) raised to the power of (Length of ID in url)
For instance, if you're only using lowercase characters (of which there are 26) and your URLs look like http://domain.com/XXXXX (for your unique id's of 5 characters), then you can make 26^5 = 11,881,376 short urls.
If you were using upper and lower case letters, you'd have 52, so 52^5 = 380,204,032 possible short URLs, et cetera.
You need to answer a number of questions, like what kinds of characters you want to allow in your set.
All letters and all digits? base 36 (5 characters can fit 2mil+)
Distinguish between upper and lowercase? That gets you to base 62 (4 characters)
Remove easily-mistaken characters and numbers (e.g. i/l 0/o)? roughly base 32 (also 5 characters)
You can often solve this kind of problem without any math wizardry.
26+26+10 = 62 characters
Try 1. 62 = 62
Try 2. 62*62 = 3,844
Try 3. 62*62*62 = 238,328
Try 4. 62*62*62*62 = 14,776,336
So 4 is your answer :)
According to the HTTP/URI Spec you can additionally use the following "unreserved characters": ALPHA / DIGIT / "-" / "." / "_" / "~"
That adds an additional 4 characters to your radix and thus
Math.log(2000000) / Math.log(66) = 3.4629721616408813
Although this still means you will end up with a 4 character URL path at maximum.