From f65e5e08ada12f819a723ff8f6d96d7dec7e0b13 Mon Sep 17 00:00:00 2001 From: Michael O'Brien Date: Tue, 31 Dec 2024 22:40:54 -0500 Subject: [PATCH] #1 - Update README.md --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5f4cecd..864efa1 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,9 @@ The 3n+1, collatz or hailstone numbers problem - https://en.wikipedia.org/wiki/C ### Optimization 1: Combine odd/even steps The following optimization will speed up a run by up to 21% -When we have an odd number, the next step is usually 3n + 1 applied to the current value. However, the number resulting from 3n + 1 will always be positive - which will require at least one divide by 2. +When we have an odd number, the next step is usually 3n + 1 applied to the current value. However, the number resulting from 3n + 1 will always be positive - which will require at least one divide by 2. If we combine the double step optimization with the fact that a shift right (or divide by 2) is always floor truncated (where the 1/2 is removed on an odd number). If we combine the floor with an implicit round up (ceil) by adding 1 (where for example 27 /2 = 13.5 = 13 rounded, with + 1 = 14) - we have the following math... + +(3n + 1) / 2 = 3/2 * n + 1/2, where we drop the 1/2 due to rounding on a shift right. We then have 3/2 * n which is also n + n/2. We add 1 to this to get a round up (it will hold as we only perform this round up for odd numbers) - of - 1 + n + n/2. The operation... ``` @@ -14,9 +16,9 @@ with a subsequent n / 2 = n >> 1 ``` -can be expressed as a single +can be expressed as a single shift right with a add + 1 or effectively a divide by ``` -n / 2 + n + 1 = n > 1 + n + 1 +n / 2 + n + 1 = n >> 1 + n + 1 ``` ### Optimization 2: Roll up all divide by 2 sequences When whe have for example a power of 2 like 256 - this will represent a straight path to 1 via 8 shift right operations.