24
2
Given list of integers {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}
.
For those who interested these numbers are used in weekday calculation.
Weekday = (m[n] + d + y + y>>2 + y/400 - y/100) % 7;
, where m[n]
- expression I'm searching, d
- day of month, y
- year - (month <= 2)
.
Construct expression consisting of arithmetic, logic and bitwise operators, which will output for positive integer n
integer m
so that m % 7
equals n-th number in the list.
Branches, ternary operators, table lookups and pointers are not allowed.
Score:
1 - for | & ^ ~ >> <<
operators
1.1 - for + - < > <= >= == != ! && ||
operators
1.2 - for *
operator
1.4 - for / %
operators
Answer with lowest score wins.
Personally I have found:
(41*n)>>4+((n+61)>>4)<<2
with score 6.4. I thought this will be hard to find so provided own expression to start with.
I guess array dereferencing (and the kin) isn't allowed either? – John Dvorak – 2014-07-16T21:48:09.110
Oh, yes of course, I have edited the question. – Somnium – 2014-07-16T21:49:43.507
6The question would be greatly improved by some motivation. Where do those numbers come from? – Peter Taylor – 2014-07-16T22:32:15.197
table lookups
Interesting phrasing I suppose... – ɐɔıʇǝɥʇuʎs – 2014-07-16T23:15:52.6034
Why not count the %7 in the score? Maybe there's another solution not using %. Is zero positive, negative, both or nothing?
– Thomas Weller – 2014-07-17T06:16:31.313@ThomasW. %7 needs to be calculated in every case. – Somnium – 2014-07-17T06:21:54.880
Yes, with the rest of the formula you have given in your updated question, this is clear now. Thanks. – Thomas Weller – 2014-07-17T06:22:43.933
@m.buettner Thank you for comment. It's my first question, I thought that own example will be good. Next time I won't steal fun) – Somnium – 2014-07-17T06:24:22.877
What language is the expression supposed to be in? From the examples, I'm assuming C or some C-like language with similar operator syntax (such as C++ or Java), but it would be nice if you could include this information in the challenge (and tag it with the appropriate language tag).
– Ilmari Karonen – 2014-07-17T09:18:25.930I want to be it in C. However any language is ok if used operators which are in C language. – Somnium – 2014-07-17T09:35:31.507