-3

I have an assigmentwhich tells me to calculate tan.

marc_xy
  • 11
  • 3
  • 3
    Is 10^2020 in radians or degrees? – Joni Dec 07 '20 at 12:26
  • Just find the right `mod`...check [here](https://stackoverflow.com/a/2177808/803359)...especially good if in degree, but in the simple version also works for rad – mikuszefski Dec 07 '20 at 14:00
  • Assignment from what class? What have you been taught so far, and what techniques do you have that might be applicable? Is this a class about numerical algorithms? Or about mathematics? Or about assembly programming? A bit more context would help with answering this (especially the "fastest" part). – Mark Dickinson Dec 07 '20 at 17:19
  • @mikuszefski: The standard square-and-multiply algorithm for binary powering is only valid for integers; it's not going to be immediately useful for computing 10**2020 modulo pi. (Fundamental identities like `x**2 % y == (x % y)**2 % y` are valid for integer `x` and `y` but break when `y` is not an integer.) The right approach here starts with an approximation to π that's valid to >2020 digits, but it's not clear whether computing such an approximation is within the scope of the OP's class. (But if the `10^2020` is in degrees, it's a much simpler problem of course.) – Mark Dickinson Dec 07 '20 at 17:21
  • @marc_xy Please confirm whether the `10^2020` is an angle in radians or degrees. This has a big impact on the complexity of the answer. – Mark Dickinson Dec 07 '20 at 17:26
  • @MarkDickinson ...yes...true, my mistake. It only works if this is in degree.....which we still do not know – mikuszefski Dec 08 '20 at 09:28
  • @MarkDickinson although I think `( x**2 ) %y == ( x * ( x % y ) ) % y` also works for non-integer. The precision is insufficient, though. – mikuszefski Dec 08 '20 at 10:15
  • @mikuszefski: I suggest testing it with a few values. It doesn't work. :-) The underlying mathematics fundamentally relies on having integers here. Try `x = 0.5` and `y = 0.5`, for example. – Mark Dickinson Dec 08 '20 at 10:21
  • @MarkDickinson ...right, I was talking about `y` being non-integer, actually assuming `x`is integer as `( 20**2 ) % pi == ( 20 * ( 20 % pi ) ) % pi` – mikuszefski Dec 08 '20 at 10:48

2 Answers2

3

The goal is to find the fastest way for making the result of a specific calculation available, note that the description does not ask for calculation within a certain range of possible input angles (or power of it, though it seems that the question might be different next year...).
Even without knowing whether the angle is given in radians or in degree (a very important detail), the answer is that the fastest method is using a constant. That has zero runtime needs and is probably even smaller in code size.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
1

The argument to tan is an angle, either in radians or degrees, and any angle greater than 2*Pi radians or 360 degrees is equivalent to an angle less than that; angles "wrap around". So, the real trick here is to find out what number in the range 0 to 2*Pi radians, or 0 to 360 degrees, the number 10^2020 corresponds to. Let's use degrees just for an illustration; the same procedure can be used for radians as well but the numbers would come out much, much, much less nice.

First, note the following:

10 ^ 10 = 10,000,000,000 ~ 280 (mod 360)

What this says is that 10 to the tenth power gives a number whose equivalent angle is 280 degrees. That means we can replace 10 to the tenth power with the number 280. Now note that

10 ^ 2020 = (10 ^ 10) ^ 202 = 280 ^ 202

We have reduced the exponent by a factor of 10; not bad. We can repeat this process by observing that

280 ^ 2 = 78,400 ~ 280 (mod 360)

This means that our angle is equivalent to 280 ^ 101 since 280 ^ 202 = (280 ^ 2) ^ 101. Indeed, even more than that...

280 ^ 101 
= 280 * 280 ^ 100 
= 280 * (280 ^ 2) ^ 50 
~ 280 * 280 ^ 50
= 280 * (280 ^ 2) ^ 25
~ 280 * 280 ^ 25
= 280 * 280 * 280 ^ 24
= 280 * 280 * (280 ^ 2) ^ 12
~ 280 * 280 * 280 ^ 12
= 280 * 280 * (280 ^ 2) ^ 6
~ 280 * 280 * 280 ^ 6
= 280 * 280 * (280 ^ 2) ^ 3
~ 280 * 280 * 280 ^ 3
= 280 * 280 * 280 * 280 ^ 2
~ 280 * 280 * 280 * 280
= (280 ^ 2) * (280 ^ 2)
~ 280 * 280
= 280 ^ 2
~ 280

So, 10 ^ 2020 ~ 280 if the argument to tan is understood to be degrees. The tangent of 280 degrees should be easy to calculate.

Patrick87
  • 27,682
  • 3
  • 38
  • 73
  • (While this gives you a way of calculating the value, check out Yunnosch's answer which is correct and likely what the person asking this question is getting at... i.e. it's probably a trick question.) – Patrick87 Dec 07 '20 at 13:32
  • Move your comment to the answer. – Peter O. Dec 07 '20 at 18:09