Counting Binary/Hexadecimal Palindromes

In my article “Counting Binary and Hexadecimal Palindromes” I derived formulas for counting binary palindromes and hexadecimal palindromes. For each type of palindrome, I derived two pairs of formulas: one pair to count n-digit palindromes, and one pair to count palindromes of n digits or less.

In this article, I will derive similar formulas to count binary/hexadecimal palindromes — multi-base palindromes I’ve shown to have an algorithmically defined structure.

Continue reading “Counting Binary/Hexadecimal Palindromes”

How to Install and Run GMP on Windows Using MPIR

To perform arbitrary-precision arithmetic in C and C++ programs on Windows, I use GMP. In particular, I use MPIR, a Windows port of GMP. MPIR is a simple alternative to using GMP under Cygwin or MinGW.

I will show you how to install MPIR in Microsoft Visual C++ as a static, 32-bit library. I will also show you how to install the optional C++ interface — also as a static, 32-bit library. I will provide two example C programs that call the GMP integer and floating-point functions, and two equivalent C++ programs — programs that use the same GMP functions, only indirectly through the C++ interface.

Continue reading “How to Install and Run GMP on Windows Using MPIR”

Counting Binary and Hexadecimal Palindromes

How many nonzero, n-digit, decimal number palindromes are there? These two formulas give the answer:

  • When n is even: 9·10n/2-1
  • When n is odd: 9·10(n+1)/2-1

How many nonzero, decimal number palindromes are there, consisting of n-digits or less? These two formulas give the answer:

  • When n is even: 2(10n/2 – 1)
  • When n is odd: 11·10(n-1)/2 – 2

So for example, there are 900 5-digit decimal palindromes, 9,000 8-digit decimal palindromes, 1,098 decimal palindromes of 5 digits or less, and 19,998 decimal palindromes of 8 digits or less.

In this article, I will derive similar formulas to count binary and hexadecimal number palindromes.

Continue reading “Counting Binary and Hexadecimal Palindromes”

Cycle Length of Powers of Five Mod Powers of Ten

In my article “Patterns in the Last Digits of the Positive Powers of Five” I noted that the positive powers of five modulo 10m cycle with period 2m-2, m ≥ 2, starting at 5m. In this article, I’ll present my proof, which has two parts:

  • Part 1 shows that the powers of five mod 2m cycle with period 2m-2, m ≥ 2, starting at 50.
  • Part 2 shows that the powers of five mod 10m cycle with the same period as the powers of five mod 2m, starting at 5m.

The highlight of my proof is in part 1, where I derive a formula to show that the period, or order, of 5 mod 2m is 2m-2. While it is in general not possible to derive a formula for the order of a number, I’ll show it is possible for the powers of five mod 2mdue to a hidden, binary structure I’ve uncovered.

Continue reading “Cycle Length of Powers of Five Mod Powers of Ten”

Patterns in the Last Digits of the Positive Powers of Five

The positive powers of five — 5, 25, 125, 625, 3125, 15625, … — have a compact, repeating pattern in their ending m digits, in the powers of five from 5m on. For example: starting with 5, their last digit is always 5; starting with 25, their last two digits are always 25; starting with 125, their last three digits alternate between 125 and 625. These cycles come in lengths of powers of two.

Cycles in the Ending Digits of the Powers of Five
Cycles in the Ending Digits of the Powers of Five

I will show you why these cycles exist, how they are expressed mathematically, and how to visualize them.

Continue reading “Patterns in the Last Digits of the Positive Powers of Five”

Seeing Powers of Five in Powers of Two and Vice Versa

The decimal representations of oppositely signed powers of two and powers of five look alike, as seen in these examples: 2-3 = 0.125 and 53 = 125; 5-5 = 0.00032 and 25 = 32. The significant digits in each pair of powers is the same, even though one is a fraction and one is an integer. In other words, a negative power of one base looks like a positive power of the other.

Powers of Two and Powers of Five that Look Alike
Powers of Two and Powers of Five that Look Alike

This relationship is not coincidence; it’s a by-product of how fractions are represented as decimals. I’ll show you simple algebra that proves it, as well as algebra that proves similar properties — in products involving negative powers.

Continue reading “Seeing Powers of Five in Powers of Two and Vice Versa”

Cycle Length of Powers of Two Mod Powers of Ten

In my article “Patterns in the Last Digits of the Positive Powers of Two” I noted that the positive powers of two modulo 10m cycle with period 4·5m-1, starting at 2m. For example, the powers of two mod 10 cycle with period four: 2, 4, 8, 6, 2, 4, 8, 6, … . In this article, I’ll present my proof, which has two parts:

  • Part 1 shows that the powers of two mod 5m cycle with period 4·5m-1, starting at 20.
  • Part 2 shows that the powers of two mod 10m cycle with the same period as the powers of two mod 5m, starting at 2m.

Continue reading “Cycle Length of Powers of Two Mod Powers of Ten”

How to Find the Last Digits of a Positive Power of Two

A common exercise in number theory is to find the last digits of a large power, like 22009, without using a computer. 22009 is a 605-digit number, so evaluating it by hand is out of the question. So how do you find its last digits — efficiently?

Modular arithmetic, and in particular, modular exponentiation, comes to the rescue. It provides an efficient way to find the last m digits of a power, by hand, with perhaps only a little help from a pocket calculator. All you need to do is compute the power incrementally, modulo 10m.

In this article, I will discuss three methods — all based on modular exponentiation and the laws of exponents — for finding the ending digits of a positive power of two. The techniques I use are easily adapted to powers of any number.

Continue reading “How to Find the Last Digits of a Positive Power of Two”

Patterns in the Last Digits of the Positive Powers of Two

The positive powers of two — 2, 4, 8, 16, 32, 64, 128, 256, … — follow an obvious repeating pattern in their ending digit: 2, 4, 8, 6, 2, 4, 8, 6, … . This cycle of four digits continues forever. There are also cycles beyond the last digit — in the last m digits in fact — in the powers of two from 2m on. For example, the last two digits repeat in a cycle of length 20 starting with 04, and the last three digits repeat in a cycle of length 100 starting with 008.

In this article, I will show you why these cycles exist, how long they are, how they are expressed mathematically, and how to visualize them.

Continue reading “Patterns in the Last Digits of the Positive Powers of Two”

Nines in Quinary

In my article “Nines in Binary”, I proved the following: positive integers of the form 10n-1, that is, integers made up of n digits of 9s, have binary representations with exactly n digits of trailing 1s. Pat Ballew made a clever observation, adapting my result to prove an equivalent statement for base 5 (quinary): positive integers of the form 10n-1 have quinary representations that have exactly n digits of trailing 4s. For example, 9999 in decimal is 304444 in quinary.

In “Nines in Binary”, I derived an expression for 10n – 1 that shows its structure as a binary number:

10n – 1 = (5n – 1) 2n + (2n – 1)

Pat derived a similar expression for 10n – 1 that shows its structure as a quinary number:

10n – 1 = (2n – 1) 5n + (5n – 1)

In essence, he swapped the 2s and 5s, making it the “dual” of my formula, if you will.

I’ll show the details of the derivation and prove why the formula works.

Continue reading “Nines in Quinary”

Exploring Binary Numbers With PARI/GP Calculator

PARI/GP is an open source computer algebra system I use frequently in my study of binary numbers. It doesn’t manipulate binary numbers directly — input, and most output, is in decimal — so I use it mainly to do the next best thing: calculate with powers of two. Calculations with powers of two are, indirectly, calculations with binary numbers.

PARI/GP is a sophisticated tool, with several components — yet it’s easy to install and use. I use its command shell in particular, the PARI/GP calculator, or gp for short. I will show you how to use simple gp commands to explore binary numbers.

PARI/GP Calculator (Example Calculations) (My Setup on Windows)
PARI/GP Calculator (Sample of Calculations Used to Explore Binary Numbers)

Continue reading “Exploring Binary Numbers With PARI/GP Calculator”

Powers Of Two In The Josephus Problem

Pradeep Mutalik of The New York Times recently blogged about a puzzle that is an instance of the Josephus Problem. The problem, restated simply, is this: there are n people standing in a circle, of which you are one. Someone outside the circle goes around clockwise and repeatedly eliminates every other person in the circle, until one person — the winner — remains. Where should you stand so you become the winner?

Here’s an example with 13 participants:

Alternating Elimination with 13 people, order of elimination shown in red (winner is person 11)
Alternating Elimination with 13 people, order of elimination shown in red (winner is person 11)

As Pradeep and his readers point out, there’s no need to work through the elimination process — a simple formula will give the answer. This formula, you won’t be surprised to hear, has connections to the powers of two and binary numbers. I will discuss my favorite solution, one based on the powers of two.

Continue reading “Powers Of Two In The Josephus Problem”