Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 142 characters in body
Source Link
rturnbull
  • 4k
  • 17
  • 27

R, (削除) 113 (削除ここまで) (削除) 100 (削除ここまで) (削除) 75 (削除ここまで) (削除) 70 (削除ここまで) (削除) 68 (削除ここまで) (削除) 67 (削除ここまで) (削除) 65 (削除ここまで) (削除) 59 (削除ここまで) 63(削除) 63 (削除ここまで) 57 bytes

functionpryr::f(N,t,l)2*l*N/t/sum(floor(runif(N)+sinpi(runif(N))*l/t)))

Try it online!

Sampling N numbers in [0, 1e6] modulo t is equivalent to simply sampling N numbers in [0, t]. Since (x1+x2)/t is equivalent to x1/t + x2/t, the first step becomes sampling from [0, t]/t, i.e. [0, 1]. Lucky for us, that is the default range for R's runif function, which returns N (real)real numbers from 0 to 1 withfrom a uniform distribution.

 runif(N)
 runif(N)
 sinpi(runif(N))*l
 floor(runif(N)+sinpi(runif(N))*l/t)
 sum(floor(runif(N)+sinpi(runif(N))*l/t))
 2*l*N/t/sum(floor(runif(N)+sinpi(runif(N))*l/t))
functionpryr::f(N,t,l)2*l*N/t/sum(floor(runif(N)+sinpi(runif(N))*l/t)))

R, (削除) 113 (削除ここまで) (削除) 100 (削除ここまで) (削除) 75 (削除ここまで) (削除) 70 (削除ここまで) (削除) 68 (削除ここまで) (削除) 67 (削除ここまで) (削除) 65 (削除ここまで) (削除) 59 (削除ここまで) 63 bytes

function(N,t,l)2*l*N/t/sum(floor(runif(N)+sinpi(runif(N))*l/t))

Sampling N numbers in [0, 1e6] modulo t is equivalent to simply sampling N numbers in [0, t]. Since (x1+x2)/t is equivalent to x1/t + x2/t, the first step becomes sampling from [0, t]/t, i.e. [0, 1]. Lucky for us, that is the default range for R's runif function, which returns N (real) numbers from 0 to 1 with a uniform distribution.

 runif(N)
 runif(N)
 sinpi(runif(N))*l
 floor(runif(N)+sinpi(runif(N))*l/t)
 sum(floor(runif(N)+sinpi(runif(N))*l/t))
 2*l*N/t/sum(floor(runif(N)+sinpi(runif(N))*l/t))
function(N,t,l)2*l*N/t/sum(floor(runif(N)+sinpi(runif(N))*l/t))

R, (削除) 113 (削除ここまで) (削除) 100 (削除ここまで) (削除) 75 (削除ここまで) (削除) 70 (削除ここまで) (削除) 68 (削除ここまで) (削除) 67 (削除ここまで) (削除) 65 (削除ここまで) (削除) 59 (削除ここまで) (削除) 63 (削除ここまで) 57 bytes

pryr::f(2*l*N/t/sum(floor(runif(N)+sinpi(runif(N))*l/t)))

Try it online!

Sampling N numbers in [0, 1e6] modulo t is equivalent to simply sampling N numbers in [0, t]. Since (x1+x2)/t is equivalent to x1/t + x2/t, the first step becomes sampling from [0, t]/t, i.e. [0, 1]. Lucky for us, that is the default range for R's runif function, which returns N real numbers from 0 to 1 from a uniform distribution.

 runif(N)
 runif(N)
 sinpi(runif(N))*l
 floor(runif(N)+sinpi(runif(N))*l/t)
 sum(floor(runif(N)+sinpi(runif(N))*l/t))
 2*l*N/t/sum(floor(runif(N)+sinpi(runif(N))*l/t))
pryr::f(2*l*N/t/sum(floor(runif(N)+sinpi(runif(N))*l/t)))
added 624 characters in body
Source Link
rturnbull
  • 4k
  • 17
  • 27

R, (削除) 113 (削除ここまで) (削除) 100 (削除ここまで) (削除) 75 (削除ここまで) (削除) 70 (削除ここまで) (削除) 68 (削除ここまで) (削除) 67 (削除ここまで) (削除) 65 (削除ここまで) 59(削除) 59 (削除ここまで) 63 bytes

As a statistical, functional programming language, it's not surprising that R is fairly well-suited to this kind of task. The fact that most functions can take vectorized input is really helpful for this problem, as rather than looping over N iterations, we just pass around vectors of size N. Thanks to @Billywob for some suggestions that lead to cutting off 4 bytes. Many thanks to @Primo for patiently explaining to me how my code wasn't working for cases where t > l, which is now fixed.

function(N,t,l)2*l*N/t/sum(floor(runif(N,,t)+sinpi(runif(N))*l>t*l/t))

The parallel lines occur at every tth point along the xEssentially this is a task in a 1-axisdimensional space, so the relativewhere we generate a line with length in [0, xl-position is] (the angle xa modulodetermines this length), and then we check to see how many times this length exceeds t. The rough algorithm is then:

  1. Sample x1 values from [0, 1000000]. Since parallel lines occur at every tth point along the x-axis, the relative x-position is x modulo t.
  2. Sample an angle a.
  3. Calculate the x2 position based on a.
  4. Check how many times x1+x2 fits into t, i.e. take the floor of (x1+x2)/t.

Sampling N numbers in [0, 1e6] modulo t is equivalent to simply sampling N numbers in [0, t]. Since (x1+x2)/t is equivalent to x1/t + x2/t, so we dothe first step becomes sampling from [0, t]/t, i.e. [0, 1]. Lucky for us, that is the default range for R's runif function, which returns N (real) numbers from 0 to 1 with a uniform distribution.

 runif(N,,t)

The nextWe repeat this step is to determine what thegenerate xa-value at, the other endangle of the needle is. To do this we need to know the angle, so we sample N real numbers in [0, 1].

 runif(N)
 sinpi(runif(N))*l

We add these twoNow we divide by xt points together, and ifadd the resultingx1 value is greater than. This yields (x1+x2)/t, then we are sure thatwhich is how far the needle crosses a line. (Recall that our relativeprotrudes from x1 value is guaranteed to be smaller than, in terms of number of parallel lines. To get the integer of how many lines were crossed, we take the tfloor.)

 floor(runif(N,,t)+sinpi(runif(N))*l>t*l/t)

In R, this returns a logical vector (e.g. TRUE FALSE TRUE TRUE). Taking the sum of a logical vector implicitly convertsWe calculate the logical values to numeric values, 1 for TRUE and 0 for FALSE, and therefore this sum is equivalent to, giving us the count c of how many lines are crossed by needles cross a line.

 sum(floor(runif(N,,t)+sinpi(runif(N))*l>t*l/t))
 2*l*N/t/sum(floor(runif(N,,t)+sinpi(runif(N))*l>t*l/t))
function(N,t,l)2*l*N/t/sum(floor(runif(N,,t)+sinpi(runif(N))*l>t*l/t))

R, (削除) 113 (削除ここまで) (削除) 100 (削除ここまで) (削除) 75 (削除ここまで) (削除) 70 (削除ここまで) (削除) 68 (削除ここまで) (削除) 67 (削除ここまで) (削除) 65 (削除ここまで) 59 bytes

As a statistical, functional programming language, it's not surprising that R is fairly well-suited to this kind of task. The fact that most functions can take vectorized input is really helpful for this problem, as rather than looping over N iterations, we just pass around vectors of size N. Thanks to @Billywob for some suggestions that lead to cutting off 4 bytes.

function(N,t,l)2*l*N/t/sum(runif(N,,t)+sinpi(runif(N))*l>t)

The parallel lines occur at every tth point along the x-axis, so the relative x-position is x modulo t. Sampling N numbers in [0, 1e6] modulo t is equivalent to simply sampling N numbers in [0, t], so we do that.

 runif(N,,t)

The next step is to determine what the x-value at the other end of the needle is. To do this we need to know the angle, so we sample N real numbers in [0, 1].

 runif(N)
 sinpi(runif(N))*l

We add these two x points together, and if the resulting value is greater than t, then we are sure that the needle crosses a line. (Recall that our relative x1 value is guaranteed to be smaller than t.)

 runif(N,,t)+sinpi(runif(N))*l>t

In R, this returns a logical vector (e.g. TRUE FALSE TRUE TRUE). Taking the sum of a logical vector implicitly converts the logical values to numeric values, 1 for TRUE and 0 for FALSE, and therefore this sum is equivalent to the count c of how many needles cross a line.

 sum(runif(N,,t)+sinpi(runif(N))*l>t)
 2*l*N/t/sum(runif(N,,t)+sinpi(runif(N))*l>t)
function(N,t,l)2*l*N/t/sum(runif(N,,t)+sinpi(runif(N))*l>t)

R, (削除) 113 (削除ここまで) (削除) 100 (削除ここまで) (削除) 75 (削除ここまで) (削除) 70 (削除ここまで) (削除) 68 (削除ここまで) (削除) 67 (削除ここまで) (削除) 65 (削除ここまで) (削除) 59 (削除ここまで) 63 bytes

As a statistical, functional programming language, it's not surprising that R is fairly well-suited to this kind of task. The fact that most functions can take vectorized input is really helpful for this problem, as rather than looping over N iterations, we just pass around vectors of size N. Thanks to @Billywob for some suggestions that lead to cutting off 4 bytes. Many thanks to @Primo for patiently explaining to me how my code wasn't working for cases where t > l, which is now fixed.

function(N,t,l)2*l*N/t/sum(floor(runif(N)+sinpi(runif(N))*l/t))

Essentially this is a task in a 1-dimensional space, where we generate a line with length in [0, l] (the angle a determines this length), and then we check to see how many times this length exceeds t. The rough algorithm is then:

  1. Sample x1 values from [0, 1000000]. Since parallel lines occur at every tth point along the x-axis, the relative x-position is x modulo t.
  2. Sample an angle a.
  3. Calculate the x2 position based on a.
  4. Check how many times x1+x2 fits into t, i.e. take the floor of (x1+x2)/t.

Sampling N numbers in [0, 1e6] modulo t is equivalent to simply sampling N numbers in [0, t]. Since (x1+x2)/t is equivalent to x1/t + x2/t, the first step becomes sampling from [0, t]/t, i.e. [0, 1]. Lucky for us, that is the default range for R's runif function, which returns N (real) numbers from 0 to 1 with a uniform distribution.

 runif(N)

We repeat this step to generate a, the angle of the needle.

 runif(N)
 sinpi(runif(N))*l

Now we divide by t and add the x1 value. This yields (x1+x2)/t, which is how far the needle protrudes from x1, in terms of number of parallel lines. To get the integer of how many lines were crossed, we take the floor.

 floor(runif(N)+sinpi(runif(N))*l/t)

We calculate the sum, giving us the count c of how many lines are crossed by needles.

 sum(floor(runif(N)+sinpi(runif(N))*l/t))
 2*l*N/t/sum(floor(runif(N)+sinpi(runif(N))*l/t))
function(N,t,l)2*l*N/t/sum(floor(runif(N)+sinpi(runif(N))*l/t))
added 78 characters in body
Source Link
rturnbull
  • 4k
  • 17
  • 27

These numbers are is interpreted as a half-turn (i.e. .5 is 90 degrees). (The OP asks for degrees from 1 to 180, but in comments it's clarified that any method is allowed if it is as or more precise.) For an angle θ, sin(θ) gives us the xx-axis distance between the ends of the needle. (Normally you'd use the cosine for something like this; but in our case, we are considering the angle θ as being relative to the y-axis, not the x-axis (that is, a value of 0 degrees goes up, not right), and therefore we use the sine, which basically phase-shifts the numbers.) Multiplied by l this gives us the x location of the end of the needle.

These numbers are is interpreted as a half-turn (i.e. .5 is 90 degrees). (The OP asks for degrees from 1 to 180, but in comments it's clarified that any method is allowed if it is as or more precise.) For an angle θ, sin(θ) gives us the x distance between the ends of the needle. (Normally you'd use the cosine for something like this; but in our case, we are considering the angle θ as being relative to the y-axis, not the x-axis (that is, a value of 0 degrees goes up, not right), and therefore we use the sine, which basically phase-shifts the numbers.)

These numbers are is interpreted as a half-turn (i.e. .5 is 90 degrees). (The OP asks for degrees from 1 to 180, but in comments it's clarified that any method is allowed if it is as or more precise.) For an angle θ, sin(θ) gives us the x-axis distance between the ends of the needle. (Normally you'd use the cosine for something like this; but in our case, we are considering the angle θ as being relative to the y-axis, not the x-axis (that is, a value of 0 degrees goes up, not right), and therefore we use the sine, which basically phase-shifts the numbers.) Multiplied by l this gives us the x location of the end of the needle.

deleted 13 characters in body
Source Link
rturnbull
  • 4k
  • 17
  • 27
Loading
added 118 characters in body
Source Link
rturnbull
  • 4k
  • 17
  • 27
Loading
added 34 characters in body
Source Link
rturnbull
  • 4k
  • 17
  • 27
Loading
added 51 characters in body
Source Link
rturnbull
  • 4k
  • 17
  • 27
Loading
added 2591 characters in body
Source Link
rturnbull
  • 4k
  • 17
  • 27
Loading
deleted 231 characters in body
Source Link
rturnbull
  • 4k
  • 17
  • 27
Loading
added 127 characters in body
Source Link
rturnbull
  • 4k
  • 17
  • 27
Loading
deleted 207 characters in body
Source Link
rturnbull
  • 4k
  • 17
  • 27
Loading
Source Link
rturnbull
  • 4k
  • 17
  • 27
Loading

AltStyle によって変換されたページ (->オリジナル) /