15
2
The goal of a Rosetta Stone Challenge is to write solutions in as many languages as possible. Show off your programming multilingualism!
The Challenge
Your challenge is to implement a program that will input a list of numbers and output the rule used to generate each successive number in the series, in as many programming languages as possible. You are allowed to use any sort of standard library function that your language has, since this is mostly a language showcase.
What is a "series?"
A series is an ordered list of integers. Each successive number in the series can be generated by applying a simple rule to the previous number in the series. In this challenge, the rule consists of multiplying the number by a constant, and then adding a second constant. Both of the constants can be any integer. The goal of this challenge is to output those two constants.
For the series 2 5 11
, the rule can be written as 2 1
. This means that each number is the previous number, times 2, plus 1. An important fact is that most series have exactly one rule. Some series have either an infinite number or none at all, but you will not have to deal with this.
Input
Input will be a list of three different integers which are the numbers in the sequence. The numbers can be either space, comma, or newline delimited, but please specify which. I am going to be flexible on this limitation because certain languages may have input restrictions. Here are four examples of input:
0 7 14
2 5 11
2 0 -4
5 -19 77
Output
Output will be two integers which represent the rule used to generate the series. The first number will be the multiplicative constant, while the second number will be the additive constant. The formatting of the output can be space, comma, or newline delimited. I am flexible on this limitation as well. Here are the corresponding examples of output:
1 7
2 1
2 -4
-4 1
The Objective Winning Criterion
As for an objective winning criterion, here it is: Each language is a separate competition as to who can write the shortest entry, but the overall winner would be the person who wins the most of these sub-competitions. This means that a person who answers in many uncommon languages can gain an advantage. Code-golf is mostly a tiebreaker for when there is more than one solution in a language: the person with the shortest program gets credit for that language.
Rules, Restrictions, and Notes
Your program can be written in any language that existed prior to April 9th, 2012. I will also have to rely on the community to validate some responses written in some of the more uncommon/esoteric languages, since I am unlikely to be able to test them.
Current Leaderboard
This section will be periodically updated to show the number of languages and who is leading in each.
- AWK (32) - mellamokb
- bash (31) - Peter Taylor
- Befunge (29) - Howard
- bc (39) - kernigh
- brainfuck (174) - CMP
- C (78) - l0n3_shArk
- C++ (96) - leftaroundabout
- Common Lisp (88) - kernigh
- Cray Chapel (59) - Kyle Kanos
- csh (86) - kernigh
- Cuda (301) - leftaroundabout
- dc (30) - kernigh
- DOS BATCH (54) - mellamokb
- Element (27) - Howard
- es (95) - kernigh
- Factor (138) - kernigh
- Felix (86) - kirbyfan64sos
- Fortran (44) - Kyle Kanos
- Go (101) - Howard
- GolfScript (16) - Howard
- Golflua (44) - Kyle Kanos
- Haskell (35) - leftaroundabout
- J (23) - Gareth
- Java (141) - Howard
- JavaScript (47) - mellamokb
- Julia (71) - M L
- Lua (51) - Howard
- Mercury (319) - leftaroundabout
- MoonScript (48) - kirbyfan64sos
- Nimrod (146) - leftaroundabout
- Owl (22) - r.e.s.
- Pascal (88) - leftaroundabout
- Perl (57) - Gareth
- PHP (61) - mellamokb
- PicoLisp (72) - kernigh
- Piet (56) - M L
- PostScript (61) - Howard
- Python (40) - Howard
- Q (36) - tmartin
- QBasic (34) - mellamokb
- R (50) - r.e.s.
- Ruby (44) - Howard
- Scala (102) - Gareth
- SQL (57) - Aman ZeeK Verma
- TI-83 BASIC (25) - mellamokb
- Unlimited Register Machine (285) - Paxinum
- VBA (57) - Gaffi
- Whitespace (123) - r.e.s.
- zsh (62) - kernigh
Current User Rankings
Equal ranks are sorted alphabetically.
Howard (9): Befunge (29), Element (27), Go (101), GolfScript (16), Java (141), Lua (51), PostScript, (61) Python, (40) Ruby (44)
kernigh (8): bc (39), Common Lisp (88), csh (86), dc (30), es (95), Factor (138), PicoLisp (72), zsh (62)
leftroundabout (6): C++ (96), Cuda (301), Haskell (35), Mercury (319), Nimrod (146), Pascal (88)
mellamokb (6): AWK (32), DOS BATCH (54), JavaScript (47), PHP (61), QBasic (34), TI-83 BASIC (41)
Gareth (3): J (23), Perl (57), Scala (102)
Kyle Kanos (3): Cray Chapel (59), Fortran (44), Golflua (44)
r.e.s. (3): Owl (22), R (50), Whitespace (123)
kirbyfan64sos (2): Felix (86), MoonScript (48)
M L (2): Julia (71), Piet (56)
Aman Zeek verma (1): SQL (57)
CMP (1): brainfuck (174)
Gaffi (1): VBA (57)
l0n3_shArk (1): C (78)
Paxinum (1): Unlimited Register Machine (285)
Peter Taylor (1): bash (31)
tmartin (1): Q (36)
It doesn't look like people actually read the tags, or other descriptions... – ceased to turn counterclockwis – 2012-04-09T17:35:43.937
@leftaroundabout: Why do you say that? I am aware (for instance) that my solution doesn't take array input and plan to fix it later. And I'm too lazy to post more than one solution at the moment :) According to the Object Winning Criterio, code-golf is the tie-breaker for two posts with the same language, hence I've included the character count for easy reference in case someone else posts a JavaScript solution. This may in fact be one of those rare moments when a question deserves both the
code-golf
andcode-challenge
tags. – mellamokb – 2012-04-09T17:36:52.533Yeah, you're right: someone has to do the not-unusual languages as well. – ceased to turn counterclockwis – 2012-04-09T18:02:27.307
I like the way you specified this problem, but mathematical pendants will tell you that a general series is not required to have a generating rule at all. – dmckee --- ex-moderator kitten – 2012-04-09T19:59:39.613
I know, but trying to calculate a generating rule wouldn't be much fun if there wasn't one. :) – PhiNotPi – 2012-04-09T20:03:31.253
3
As long as you keep the Current Leaderboard in the same standard format as above, you can use this to generate each user's scores: http://jsfiddle.net/bk2WM/2/
– mellamokb – 2012-04-10T14:43:43.493@mellamokb Wow, thanks. As the number of languages increases, that could actually become very useful. – PhiNotPi – 2012-04-10T14:59:00.893
Added a score-board to my post from the current list. – mellamokb – 2012-04-11T13:49:11.183
1
My newest version (http://jsfiddle.net/bk2WM/4/) provides a raw output (in the textarea) you can copy/paste into a post, and formats it like I did in my post. Feel free to change/play with the layout.
– mellamokb – 2012-04-11T14:26:34.787Doesn't my PostScript solution count? ;-) – Howard – 2012-04-11T16:27:10.503
@Howard Sorry, I must have overlooked it. – PhiNotPi – 2012-04-11T17:43:15.247