# Michael J. Swart

## July 24, 2017

### Solve this Math Puzzle using SQL

Filed under: Technical Articles — Michael J. Swart @ 9:08 am

IBM holds a monthly math/programming challenge called “Ponder This”.
This month’s problem (July 2017) is an interesting one because it can (mostly) be solved using SQL and highschool math:

“Find a way to assign a unique digit to each letter such that the 863,172nd digit after the decimal point of sin(PONDER) and of sin(THIS) are not zeros, but more than a fifth of the first million digits of sin(PONDER)*sin(THIS) are zeros.

“The parameters are in degrees, and we count the digits from 1, so for example, sin(100) = 0.9848077530122… so the fifth digit after the decimal point of sin(100) is zero.”

If you’re up to the challenge, use any technique you like, but you’ve only got a week. Next week (August 1st) after IBM publishes their solution, I’ll publish the SQL based solution that I used.

### Update: My Solution

First I realized that PONDER and THIS represent integers greater than 360 degrees. That’s okay. All I need to do is find two numbers A and B between 1 and 360 with the desired properties and then take that solution and add 360 to either number until I get a six digit number and a four digit number with no shared digits.

So my guess is that `SIN(A) * SIN(B)` is a regular number.
I can look for that using SQL:

```with Nums as ( select top 360 row_number() over (order by (select 1)) as n from sys.syscolumns ) select A.n as a, B.n as b, sinA, sinB, sinA * sinB as sinA_x_sinB from Nums A cross join Nums B cross apply ( select SIN(RADIANS(A.n + 0.0)) as sinA, SIN(RADIANS(B.n + 0.0)) as sinB ) calc where right(format(sinA * sinB, '###.00000000000'), 4) = '0000' and right(format(sinA, '###.00000000000'), 4) <> '0000' and right(format(sinB, '###.00000000000'), 4) <> '0000' and A.n % 10 <> B.n % 10 order by A.n, B.n```

Notice the one extra filter I snuck in there: `A.n % 10 <> B.n % 10`. If A and B share the last digit, then PONDER and THIS would share the last digit and we can’t have that.

I’m going to focus on one solution, `A = 54`, and `B = 18`.
Can I add 360 to each of these to get an 6 digit and a 4 digit solution for PONDER and THIS? Yes.

```select TOP (27) 54 + 360 * row_number() over (order by (select 1)) as id into #nums4 from sys.messages a; delete #nums4 where id < 1000;   select TOP (2777) 18 + 360 * row_number() over (order by (select 1)) as id into #nums6 from sys.messages a; delete #nums6 where id < 100000;   select #nums4.id, #nums6.id from #nums4 cross join #nums6 where dbo.HasDuplicate(10000*#nums6.id+#nums4.id) = 0```

Where `HasDuplicate` is a CLR function I wrote that returns whether a number has repeat digits.
This gives me a few solutions, for example `PONDER = 102978` and `THIS = 3654`.

I get a kick out of these puzzles and as an added bonus, I get to exercise my SQL skills.

### Update 2

Stephen pointed out in the comments that I didn’t even check to see whether the 863,172nd digit of `SIN(PONDER)` or `SIN(THIS)` is nonzero. How did I do that? Well, I didn’t use SQL. I used google. Searching for “What’s the exact value of SIN(18)” gives me this page Exact Value of sin 18°. The internet’s amazing. So I trust that the exact value is `sin(18) = ( -1 + sqrt(5) ) / 4` which has the same 863,172nd digit as `sqrt(5) / 4`. The same reasoning works for `SIN(54)` because `sin(54) = ( 1 + sqrt(5) ) / 4`.

Rather than figure out how to calculate the 863,172nd digit of `sqrt(5) / 4`, I searched for and found the first million digits of sqrt(5). The internet’s amazing. I know that when you divide a number by four, any digit in that number usually has a limited scope of influence on the digits in the answer. I divided the digits around the 863172 place by 4 to convince myself that the 863172nd digit of `sqrt(5) / 4` is not 0.

1. You haven’t yet proven your solution satisfies the first part of the puzzle 🙂 All other things being equal (of course they’re not), there’s about 19% chance a solution like this fails.

Comment by Stephen — August 2, 2017 @ 8:47 pm

2. Good point Stephen,
I could have left that part as an exercise for the reader, but I updated the post to include that part.

Comment by Michael J. Swart — August 3, 2017 @ 9:05 am