How to generate a power sequence of two in R?

R ProgrammingServer Side ProgrammingProgramming

In R, for the calculation of power we can simply use power operator ^ and this will be also used in case of generating a power sequence. For example, if we want to generate a power sequence from 1 to 5 of 2 then we can use the code 2^(1:5) this will result 2 4 8 16 32.

Example

 Live Demo

2^(0:2)

Output

[1] 1 2 4

Example

 Live Demo

2^(0:10)

Output

[1] 1 2 4 8 16 32 64 128 256 512 1024

Example

 Live Demo

2^(0:50)

Output

[1] 1.000000e+00 2.000000e+00 4.000000e+00 8.000000e+00 1.600000e+01
[6] 3.200000e+01 6.400000e+01 1.280000e+02 2.560000e+02 5.120000e+02
[11] 1.024000e+03 2.048000e+03 4.096000e+03 8.192000e+03 1.638400e+04
[16] 3.276800e+04 6.553600e+04 1.310720e+05 2.621440e+05 5.242880e+05
[21] 1.048576e+06 2.097152e+06 4.194304e+06 8.388608e+06 1.677722e+07
[26] 3.355443e+07 6.710886e+07 1.342177e+08 2.684355e+08 5.368709e+08
[31] 1.073742e+09 2.147484e+09 4.294967e+09 8.589935e+09 1.717987e+10
[36] 3.435974e+10 6.871948e+10 1.374390e+11 2.748779e+11 5.497558e+11
[41] 1.099512e+12 2.199023e+12 4.398047e+12 8.796093e+12 1.759219e+13
[46] 3.518437e+13 7.036874e+13 1.407375e+14 2.814750e+14 5.629500e+14
[51] 1.125900e+15

Example

 Live Demo

2^(1:20)

Output

[1] 2 4 8 16 32 64 128 256 512
[10] 1024 2048 4096 8192 16384 32768 65536 131072 262144
[19] 524288 1048576

Example

 Live Demo

2^(1:35)

Output

[1] 2 4 8 16 32 64
[7] 128 256 512 1024 2048 4096
[13] 8192 16384 32768 65536 131072 262144
[19] 524288 1048576 2097152 4194304 8388608 16777216
[25] 33554432 67108864 134217728 268435456 536870912 1073741824
[31] 2147483648 4294967296 8589934592 17179869184 34359738368

Example

 Live Demo

2^(1:45)

Output

[1] 2.000000e+00 4.000000e+00 8.000000e+00 1.600000e+01 3.200000e+01
[6] 6.400000e+01 1.280000e+02 2.560000e+02 5.120000e+02 1.024000e+03
[11] 2.048000e+03 4.096000e+03 8.192000e+03 1.638400e+04 3.276800e+04
[16] 6.553600e+04 1.310720e+05 2.621440e+05 5.242880e+05 1.048576e+06
[21] 2.097152e+06 4.194304e+06 8.388608e+06 1.677722e+07 3.355443e+07
[26] 6.710886e+07 1.342177e+08 2.684355e+08 5.368709e+08 1.073742e+09
[31] 2.147484e+09 4.294967e+09 8.589935e+09 1.717987e+10 3.435974e+10
[36] 6.871948e+10 1.374390e+11 2.748779e+11 5.497558e+11 1.099512e+12
[41] 2.199023e+12 4.398047e+12 8.796093e+12 1.759219e+13 3.518437e+13

Example

 Live Demo

2^(11:20)

Output

[1] 2048 4096 8192 16384 32768 65536 131072 262144 524288
[10] 1048576

Example

 Live Demo

2^(101:120)

Output

[1] 2.535301e+30 5.070602e+30 1.014120e+31 2.028241e+31 4.056482e+31
[6] 8.112964e+31 1.622593e+32 3.245186e+32 6.490371e+32 1.298074e+33
[11] 2.596148e+33 5.192297e+33 1.038459e+34 2.076919e+34 4.153837e+34
[16] 8.307675e+34 1.661535e+35 3.323070e+35 6.646140e+35 1.329228e+36

Example

 Live Demo

2^(2:15)

Output

[1] 4 8 16 32 64 128 256 512 1024 2048 4096 8192
[13] 16384 32768

Example

 Live Demo

2^(sample(0:5,100,replace=TRUE))

Output

[1] 8 16 16 4 4 2 2 16 8 2 16 32 8 1 8 32 8 1 32 32 32 2 4 4 32
[26] 8 2 32 2 4 32 1 32 16 8 16 32 1 8 1 2 1 2 32 4 8 8 32 1 4
[51] 8 2 8 8 2 4 32 4 32 32 4 8 8 4 8 1 16 2 32 16 1 16 1 1 8
[76] 16 1 4 8 4 1 1 2 16 1 2 16 8 32 16 4 16 8 32 8 1 8 32 2 1

Example

 Live Demo

2^(sample(1:10,100,replace=TRUE))

Output

[1] 1024 4 16 256 2 32 4 4 2 8 256 128 2 32 128
[16] 16 32 16 64 512 128 16 32 256 64 1024 8 64 2 4
[31] 2 1024 16 4 4 128 8 32 256 2 8 4 16 2 32
[46] 1024 64 1024 16 512 8 512 8 1024 4 4 16 1024 2 4
[61] 128 512 1024 128 512 32 4 16 64 128 64 16 32 1024 8
[76] 8 512 2 64 64 64 128 32 2 128 512 512 64 512 1024
[91] 16 8 8 512 2 2 256 8 32 128

Example

 Live Demo

2^(sample(11:15,100,replace=TRUE))

Output

[1] 4096 2048 16384 4096 4096 4096 32768 8192 32768 2048 2048 32768
[13] 16384 8192 2048 32768 8192 8192 32768 16384 8192 16384 2048 8192
[25] 4096 4096 16384 2048 16384 16384 8192 4096 2048 2048 16384 16384
[37] 32768 8192 8192 2048 32768 2048 8192 2048 4096 8192 4096 8192
[49] 32768 4096 8192 16384 32768 2048 16384 16384 32768 8192 2048 4096
[61] 8192 2048 2048 16384 2048 8192 8192 2048 16384 16384 8192 32768
[73] 8192 16384 8192 8192 8192 2048 4096 16384 4096 32768 32768 8192
[85] 2048 32768 8192 8192 16384 4096 8192 32768 16384 2048 32768 8192
[97] 2048 2048 32768 2048
raja
Published on 17-Oct-2020 15:44:51
Advertisements