Saturday, November 5, 2016

PowerShell Modulus operator is a remainder operator, not a modulus operator

The percent sign in PowerShell is known as the modulus operator. But it is in fact not a modulus operator. It is a remainder operator.

What’s the difference? Most of the time, nothing.

If both operands are positive, modulus and remainder are equivalent.

16 modulo 7 is 2, and the remainder of 16 divided by 7 is 2.

In some calculations used to find an index in a PowerShell array using negative operands, modulus and remainder are mathematically different, but functionally equivalent.

Sometimes in PowerShell, the “wrong” result works anyway.

For example, given the following function:

function Get-DayofWeek ( $StartDay = 0, $Offset = 0 )
{
$Weekdays = "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
$EndDay = ( $StartDay + $Offset ) % 7
return $Weekdays[$EndDay]
}

Get-DayofWeek -StartDay 0 -Offset  16
Get-DayofWeek -StartDay 0 -Offset -16

We are using % to calculate the index to the correct day of the week in the array. If the starting day (day zero) is on Sunday, day 16 will be on 16 modulo 7, or 2, which points to Tuesday.

If we count backwards 16 days from Sunday, -16 modulo 7 is 5, which points to Friday.

But % doesn’t do modulo, it finds the remainder, and the remainder of -16 divided by 7 is -2.

But it works anyway. In PowerShell, $WeekDays[5] points to the sixth element from the start of the array, and $WeekDays[-2] points to the second element from the end of the array. In an array with 7 elements, both of these point to the same element.

Modulus and remainder handle negative dividends differently

If we need to use the results from negative operands anywhere other than as the index of an array, we need to be aware of the difference between modulus and remainder, and understand that % performs a remainder operation.

For positive operands, modulus and remainder follow the same pattern.

%
DividendDivisorModulusRemainder
7311
6300
5322
4311
3300
2322
1311
0300

The difference is in what happens to that pattern when we carry on past zero.

For modulus, the pattern continues unchanged.

For remainder, the pattern reverses, and becomes negative.

%
DividendDivisorModulusRemainder
7311
6300
5322
4311
3300
2322
1311
0300
-132-1
-231-2
-3300
-432-1
-531-2
-6300
-732-1

Changing the sign of the divisor does not change the result of either modulus or remainder.

%
DividendDivisorModulusRemainder
7-311
6-300
5-322
4-311
3-300
2-322
1-311
0-300
-1-32-1
-2-31-2
-3-300
-4-32-1
-5-31-2
-6-300
-7-32-1

Wednesday, October 12, 2016

Azure Automation hybrid workers require only outbound port 443

Implementing Azure Automation (AzA) hybrid workers is now easier. While it is a technical improvement that is streamlining implementation, it is mostly the politics of implementation that have been streamlined.

Last year the Azure Automation team introduced so-called hybrid workers. With this feature, the AzA database and management services still reside in the Microsoft cloud, but some or all of the work can be performed on servers in your datacenter (or in a different cloud) and which you control.

One of the original challenges to implementing hybrid workers was all of the ports that had to be opened between the hybrid workers and the management components in the cloud. Much of it was easy. The hybrid worker could be fully configured and reporting to the core of the management system using only port 443. But to actually pick up work, the hybrid workers needed to talk to the Azure Service Bus, which required opening up a whole lot of ports to a whole lot of IP addresses. Some network security teams get cranky when you ask them to do that.

Recent updates modified the network requirements, and radically simplified the port requirements.

Now, the only hole that needs to be opened on the corporate firewall is outgoing TCP on port 443. It is now a simple, well understood, reasonable request to the network security team. And in most companies, that connectivity is already in place.

Much easier.

Monday, September 12, 2016

Notes on Parker chains


This is a reponse to one of Matt Parker’s videos on his StandUpMaths YouTube channel at https://www.youtube.com/watch?v=LYKn0yUTIU4

Matt talked about a function defined for non-negative integers wherein the result is the number of letters in the name of the number.

For example,

`F(23) = 11` (11 letters in “twenty three”)
`F(11) = 6` (6 letters in “eleven”)
`F(6) = 3` (3 letters in “six”)
`F(3) = 5` (5 letters in “three”)
`F(5) = 4` (4 letters in “five”)

And that’s the end of the Parker chain, because `F(4) = 4`.

In English, all starting points eventually lead to 4.

Matt showed us that 23 is the lowest number that starts a Parker chain with a length of 6.

I am defining `C_n` as the lowest number starting a Parker chain of length n using the Parker algorithm.

`C_6 = 23`

Matt challenged us to find `C_7`, and to find any other interesting information about this algorithm, and to post any such findings as a comment on his video. I got a little bit carried away, and wrote a little more than will fit comfortably in a comment.

Assuming use of the short scale (for now) and not using the word “and”.

The longest number name under `10^30` is 373,373,373,373,373,373,373,373,373,373, or three hundred three hundred seventy-three octillion three hundred seventy-three septillion three hundred seventy-three sextillion three hundred seventy-three quintillion three hundred seventy-three quadrillion three hundred seventy-three trillion three hundred seventy-three billion three hundred seventy-three million three hundred seventy-three thousand three hundred seventy-three, with 321 alphabetic characters.

Therefore, in the longest possible Parker chain whose first number is `10^30` or lower, the second number in the chain is 321 or lower. All Parker chains starting with 321 or lower have 6 or fewer numbers in the chain. Therefore, the longest possible Parker chain whose first number is `10^30` or lower has a maximum of 7 numbers.

The first Parker chain of 7 numbers is 323, 23, 11, 6, 3, 5, 4.

`C_7 = 323`

How strange is that? Out of all of the possible starting numbers 30 digits or less, you only have to try as high as 323 to find an example Parker chain of the maximum possible length in that range.

As others have noted, the first Parker chain of 8 numbers has a starting number of just a bit over `10^30`.

`C_8 ~~ 10^30`

That’s a rather large gap.

Which makes me wonder, how large is the next gap?

Within each power of a thousand, the longest number name is approximately 32 letters longer than the longest number name in the previous power of a thousand. That’s an additional 24 letters for “three hundred seventy-three” and an average of 8 letters for the appropriate “*illion” for that power.

(There are many other, possibly infinite, numbers with the same name length, but a number with “373” repeating will be the lowest number of that name length.)

From that we can derive a simple formula to find the approximate location of the start of the next longer Parker chain.

`C_(n+1)` has as least `C_n` letters in its name. The first number with `C_n` letters in its name will be approximately

`C_(n+1) ~~ 10^(3/32 C_n)`

We can see this works for `C_7 = 323`.

`C_8 ~~ 10^(3/32 323)`
`C_8 ~~ 10^30`

And we can use that to estimate the size of the next gap.

At this scale, when `C_n` in is the form `10^x`, we can simplify the approximation.

`C_(n+1) ~~ 10^(3/32 10^x)`
`C_(n+1) ~~ 10^(1/10 10^x)`
`C_(n+1) ~~ 10^(10^(x-1))`

`C_9 ~~ 10^(10^(30-1))`
`C_9 ~~ 10^(10^29)`

That’s quite a gap.

Now we’re at a scale where we can simplify the approximation even farther, as our x in `10^x` is so large that subtracting one from it is a trivial difference. So for very large numbers:

`C_(n+1) ~~ 10^(C_n)`

From here on out, the gaps are easy to predict.

`C10 ~~ 10^(10^(10^29))`
`C11 ~~ 10^(10^(10^(10^29)))`
`C12 ~~ 10^(10^(10^(10^(10^29))))`
`C13 ~~ 10^(10^(10^(10^(10^(10^29)))))`

Now you may have noticed I made an assumption early on that you might take issue with. I am assuming an average of 8 letters for the name of each group of three digits, the so-called illion word. But it is actually reasonable.

As going to `C_9` takes us well beyond the range of numbers in any existing number naming system, we need a hypothetical number naming system that can handle these ridiculously large numbers. A reasonable extension of existing systems is possible.

To get up to `10^(10^29)`, we can’t just keep making up new illion words. We’ll run out of possibilities long before we get there. So we need something different. We already have something that can work, the so-called Long System previously used in Britain and still used in the rest of Europe and parts of Canada. We just need to follow it to a different logical conclusion than was done in the past.

Here is the Short System, used in the US, Britain and parts of Canada.

Short system
`1000^0``10^0`
`1000^(2^0)``1000^1``10^3`thousand
`1000^(2^1)``1000^2``10^6`million
`1000^3``10^9`billion
`1000^(2^2)``1000^4``10^12`trillion
`1000^5``10^15`quadrillion
`1000^6``10^18`quintillion
`1000^7``10^21`sextillion
`1000^(2^3)``1000^8``10^24`septillion
`1000^9``10^27`octillion
`1000^10``10^30`nonillion

Compare that to the Long System. I like the long system. Mathematically it is more logical, with major names based on powers of one million. And we can use it for much larger numbers without needing to add new words.

Short systemLong system
`1000^0``10^0`
`1000^(2^0)``1000^1``10^3`thousandthousand
`1000^(2^1)``1000^2``10^6`millionmillion
`1000^3``10^9`billionthousand million
`1000^(2^2)``1000^4``10^12`trillionbillion
`1000^5``10^15`quadrillionthousand billion
`1000^6``10^18`quintilliontrillion
`1000^7``10^21`sextillionthousand trillion
`1000^(2^3)``1000^8``10^24`septillionquadrillion
`1000^9``10^27`octillionthousand quadrillion
`1000^10``10^30`nonillionquintillion
`1000^11``10^33`thousand quintillion
`1000^12``10^36`sextillion
`1000^13``10^39`thousand sextillion
`1000^14``10^42`septillion
`1000^15``10^45`thousand septillion
`1000^(2^4)``1000^16``10^48`octillion
`1000^17``10^51`thousand octillion
`1000^18``10^54`nonillion
`1000^19``10^57`thousand nonillion
`1000^20``10^60`

But that only gets us twice as far, and we need to go much, much farther. Here is my hypothetical Really Long System. It starts the same as the long system, but more than one pattern can start from there, and I took the one less traveled by.

Short systemLong systemReally long system
`1000^0``10^0`
`1000^(2^0)``1000^1``10^3`thousandthousandthousand
`1000^(2^1)``1000^2``10^6`millionmillionmillion
`1000^3``10^9`billionthousand millionthousand million
`1000^(2^2)``1000^4``10^12`trillionbillionbillion
`1000^5``10^15`quadrillionthousand billionthousand billion
`1000^6``10^18`quintilliontrillionmillion billion
`1000^7``10^21`sextillionthousand trillionthousand million billion
`1000^(2^3)``1000^8``10^24`septillionquadrilliontrillion
`1000^9``10^27`octillionthousand quadrillionmillion trillion
`1000^10``10^30`nonillionquintillionthousand trillion
`1000^11``10^33`thousand quintillionmillion trillion
`1000^12``10^36`sextillionthousand million trillion
`1000^13``10^39`thousand sextillionbillion trillion
`1000^14``10^42`septillionthousand billion trillion
`1000^15``10^45`thousand septillionmillion billion trillion
`1000^(2^4)``1000^16``10^48`octillionthousand million billion trillion
`1000^17``10^51`thousand octillionquadrillion
`1000^18``10^54`nonillionthousand quadrillion
`1000^19``10^57`thousand nonillionmillion quadrillion
`1000^20``10^60`thousand million quadrillion
`1000^21``10^63`billion quadrillion
`1000^22``10^66`thousand billion quadrillion
`1000^23``10^69`million billion quadrillion
`1000^24``10^72`thousand million billion quadrillion
`1000^25``10^75`thousand trillion quadrillion
`1000^26``10^78`million trillion quadrillion
`1000^27``10^81`thousand million trillion quadrillion
`1000^28``10^84`billion trillion quadrillion
`1000^29``10^87`thousand billion trillion quadrillion
`1000^30``10^90`million billion trillion quadrillion
`1000^31``10^93`thousand million billion trillion quadrillion
`1000^(2^5)``1000^32``10^96`quintillion
`vdots`
`1000^(2^6)``1000^64``10^99`sextillion
`1000^(2^7)``1000^128``10^102`septillion
`1000^(2^8)``1000^256``10^768`octillion
`1000^(2^9)``1000^512``10^1536`nonillion

In the short system, with every new illion term added, we can count up through another power of a thousand.

In the long system, with every new illion term added, we can count up through another 2 powers of a thousand.

In the really long system, it’s an exponential growth rate. With every new illion term added, we can count up through the square of the previous word. That is, we double the number of powers of a thousand we can count through.

With this system, we only need 95 illion words (and a really long time) to count all the way up to `10^(10^29)`.

So this is a system that hypothetically would work.

In the table above, it looks like some of those names are quite a bit longer than 8 letters, but they are only used for a single group like that when talking about round numbers. For example, this number 1,000,000,000,000,000,000,000 would “one thousand million billion” but this number 1,002,003,004,005,006,007,008 would be “one thousand two million three thousand four billion five thousand six million seven thousand eight”

So with this system we do maintain an average of eight letters per power of a thousand group into very, very large numbers. Eventually we will be forced to start using longer illion words that will drive the average above 8 letters, but by then the approximate numbers we are describing will be so monstrous in size that the increase will be relatively trivial.