2
$\begingroup$

This is not homework, I am a programmer and I encountered this problem in the implementation of an algorithm of mine.

I was wondering if the following can be done without the use of any auxiliary data structure (such as arrays) and without if statements or variable length loops, just by using some smart mathematical approach. I also thought about interpolation, but that seems to beat the purpose for this set of data.

Auxiliary variables can be used, but only such that they can contain an integer at most.


Is there a way to map all the numbers in the sets to the indicated value through the use of some function ? Maybe modulo operations ?

$S_0 = \text{{0, 3,6,27,30,33,54,57,60}} \rightarrow 0$

$S_1 = \text{{1, 4,7,28,31,34,55,58,61}} \rightarrow 1$

$S_2 = \text{{2, 5,8,29,32,35,56,59,62}} \rightarrow 2$

$S_3 =\text{{9, 12,15,36,39,42,63,66,69}} \rightarrow 3$

$S_4 =\text{{10, 13,16,37,40,43,64,67,70}} \rightarrow 4$

$S_5 =\text{{11, 14,17,38,41,44,65,68,71}} \rightarrow 5$

$S_6 =\text{{18, 21,24,45,48,51,72,75,78}} \rightarrow 6$

$S_7 =\text{{19, 22,25,46,49,52,73,76,79}} \rightarrow 7$

$S_8 =\text{{20, 23,26,47,50,53,74,77,80}} \rightarrow 8$


Basically I'm looking for a function $f$ that satisfies $f(s_{ij}) =i $ for any element $s_{ij}$ in the set $S_i$ that does the minimum number of computations.

I've been trying to connect the patterns for almost $3$ hours now, if anyone can help, I would be most grateful.

$\endgroup$
2
$\begingroup$

It seems like your patterns come in blocks. If x is the number you want to de-index, you can first modulo it to get the block number, and the index within that block

a = x % 9
b = x / 9

$a$ is the block number. You can use a simple function c = 3 * a%3 to map $a = 0,1,2,3,4,5,6,7,8$ to the "initial row" of block a $c = 0,3,6,0,3,6,0,3,6$.

Similarly, map $b = 0,1,2,3,4,5,6,7,8$ to $d = 0,1,2,0,1,2,0,1,2$. $c+d$ then gives you the desired result.

Edit: David K pointed out that $a$ and $b$ should be reversed, so $$ f(x) = 3(x/9)\% 3 + x\%3 $$

$\endgroup$
  • $\begingroup$ This formula works. Applying modulo twice to $a$ is unnecessary. Writing it as a function (and dropping unneeded mod) the answer is: $$f (x)=3(x\%9)+(x/9)\%3$$ $\endgroup$ – Ian Miller Mar 19 '16 at 2:55
  • $\begingroup$ @IanMiller I applied it twice so it can be generalised for larger blocks. $\endgroup$ – Henricus V. Mar 19 '16 at 2:57
  • $\begingroup$ Sorry, that wasn't intended as a criticism just as an observation. $\endgroup$ – Ian Miller Mar 19 '16 at 2:58
  • $\begingroup$ @IanMiller, sorry, am I missing something ? $f(80) = 26$ when it should be $f(80) = 8$.. $\endgroup$ – programmer101 Mar 19 '16 at 3:01
  • $\begingroup$ @programmer101 I think the first modulo should be $3$. $\endgroup$ – Henricus V. Mar 19 '16 at 3:03
1
$\begingroup$

Let $f(s)=s \pmod 3 +3[(s \pmod {27})/9]$ where the division is integer division-ignore any remainder.

$\endgroup$
  • 1
    $\begingroup$ This doesn't seem to work for quite a few values $\endgroup$ – programmer101 Mar 19 '16 at 2:37
  • $\begingroup$ Putting (mod n) in parentheses doesn't mean to reduce anything. It means that, for an entire equation, values that are the same, modulo n, are equivalent. For example, 5 ≡ 7 (mod 2) doesn't do anything to either 5 or 7. $\endgroup$ – Olathe Mar 19 '16 at 2:48
  • $\begingroup$ @Olathe, your tone is inappropriate at best, I think you should try and read again . I think it's fairly obvious just by looking at Ross' reputation that he doesn't need explanations like this one, since you're wrong. For your information , "Given two positive numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder of the Euclidean division of a by n." i.e. $ 5 mod 3 = 2 $. $\endgroup$ – programmer101 Mar 19 '16 at 2:55
  • $\begingroup$ Canceling the anonymous downvote by a upvote $\endgroup$ – Shailesh Mar 19 '16 at 2:59
  • $\begingroup$ @programmer101 Yes, a mod n means that, but you don't put binary operations at the start of something in parentheses. For example, no one should write 5 (+ 6). When it starts off in parentheses like that, it means that both sides are equivalent, modulo the modulus. See, for example, math.stackexchange.com/a/16153 $\endgroup$ – Olathe Mar 19 '16 at 3:01
0
$\begingroup$

I find it easiest to describe the problem in terms of a function. You want a function $f(n)$ such that if $n$ is in the set $S_k$, then $f(n) = k$. In other words, $f$ maps each element of $S_0$ to $0$, each element of $S_1$ to $1$, etc.

The pattern repeats in blocks of $27$ numbers. That is, the sequence of values $f(27), f(28), f(29), f(30), \ldots, f(53)$ is an exact repetition of the sequence of values $f(0), f(1), f(2), f(3), \ldots, f(26)$.

So, first step, let $p = n \bmod 27$, where $a \bmod b$ is defined as the remainder when integer $a$ is divided by non-zero integer $b$ using only an integer quotient, no fractions. (This is p = n % 27 in C and other programming languages that have that kind of "modulo" or "remainder" operator). For non-negative values of $n$ (which is all we have to deal with here), this implies that $0 \leq p < 27$.

Now let $q = \left\lfloor \frac p9 \right\rfloor$ (q = p / 9 if p and q are declared "integer" and your programming language rounds integer division toward zero). Then $q$ is one of the three values $0,1,2$, identifying whether $p$ is in the first three sets $S_0,S_1,S_2$ ($0 \leq p \leq 8$), the middle three sets $S_3,S_4,S_5$ ($9 \leq p \leq 17$), or the last three sets $S_6,S_7,S_8$ ($18 \leq p \leq 26$). (Keep in mind that $n$ will be in the same set as $p$.)

Now let $r = p \bmod 3$ or $r = n \bmod 3$ (it will be the same result either way). Then $r$ is one of the three values $0,1,2$, identifying which of the three sets $p$ lies in within the group of three sets identified by $q$.

Finally, putting it all together: $$ f(n) = 3q + r = 3 \left\lfloor \frac{n \bmod 27}{9} \right\rfloor + n \bmod 3. $$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.