r/dailyprogrammer • u/nint22 1 2 • May 28 '13
[05/28/13] Challenge #127 [Easy] McCarthy 91 Function
(Easy): McCarthy 91 Function
The McCarthy 91 Function is a recursive function which, given an integer N, returns the integer 91 if N is equal to or smaller than 100, or simply N-10 if N is greater than 100. Sounds simple, but look at the function definition in the linked Wikipedia article! How could such a function work to always return a constant (for N <= 100) that isn't in the function body? Well, that's your task: write out each step that McCarthy's function does for a given integer N.
Author: nint22
Formal Inputs & Outputs
Input Description
You will be given a single integer N on standard console input. This integer will range between 0 and 2,147,483,647 (without commas).
Output Description
You must output what the function does on each recursion: first you must print the function the expression that is being computed, and then print which condition it took. Simply put, you must print each recursion event in the following string format: "<Expression being executed> since <is greater than | is equal to or less than> 100". Note that for the first line you do not need to print the "since" string (see example below). You should also print the final expression, which is the result (which should always be 91).
Sample Inputs & Outputs
Sample Input
Note: Take from Wikipedia for the sake of keeping things as simple and clear as possible.
99
Sample Output
M(99)
M(M(110)) since 99 is equal to or less than 100
M(100) since 110 is greater than 100
M(M(111)) since 100 is equal to or less than 100
M(101) since 111 is greater than 100
91 since 101 is greater than 100
91
3
u/Hapins May 29 '13
My C++
include <iostream>
int main(){
}
3
u/Pistii May 30 '13
2
u/ndstumme Jun 06 '13
I realize this is from last week, but I'm hoping some people are still lurking here. I don't understand the initial input.
Will the program always be initiated with an N less than 101, or can it be initiated with 4,560,830? It seems to me that if you start with the latter, it would just return 4,560,820 and stop.
Either I'm missing something about how the function works (and how everyone's answers work), or the Formal Input Description on this challenge is incorrect.
7
u/Rapptz 0 0 May 28 '13
This doesn't exactly follow the rules (there's no output or anything) but I wanted to do some template meta programming (that is, all the computations happen at compile time).
C++11
6
u/otsojaun May 29 '13
My java solution.
Also my first submission and challenge here. Nice subreddit!
5
u/tchakkazulu 0 2 May 29 '13 edited May 29 '13
Looking at the other programs, from what I can see, a lot of them suffer from the same problem I pointed out on atomic_cheese's solution. (I responded to that one, as Haskell is my native language, so that was code I immediately understood :p). What is the expected outcome when being fed 80? When being fed 91? Is 91 the ending condition, or is "No more applications of
Mleft" the ending condition? Actually printing the reduction steps may be a tad bit too complicated for an Easy challenge.Anyway, I have two Haskell solutions, both implemented as instances of a typeclass. First the code that's common to both solutions:
The solutions differ in that they use a different implementation for the
Exprtypeclass.Here is the solution that came immediately to me. It's a direct implementation of the reduction of
Musing an algebraic datatype for the expression that closely corresponds to the actual structure of applications ofM. Hence ExprA (for Algebraic).After having written this, I took another look at the
Exprdatatype. Assuming we only ever applyMa finite amount of times (which is indeed the case, when starting with a singleM), an expression always ends with an integer. We may as well encode expressions as the amount ofMs, and theIntat the end of it:4
u/Coder_d00d 1 3 May 29 '13
Javascript embedded in an html document and using Jquery to display my results on the webpage. Reloading the page allows the program to re-run.
Example output entering "99" in the prompt box:
4
u/Mr_Dark May 29 '13 edited May 29 '13
C# version
1
u/montas May 28 '13
According to Wikipedia, this function returns 91 for input smaller then 101, for input larger than that, it simply returns (input - 10)
For better idea, check Wolfram with graph.
So it is not "always returns the integer 91"
3
u/t-j-b May 29 '13 edited May 29 '13
JavaScript solution, prints the recursion level and condition passed
3
u/pandubear 0 1 May 30 '13 edited May 30 '13
Common Lisp! Any feedback? I'm pretty new to Lisp...
3
u/analmightywizard May 30 '13 edited May 30 '13
using PHP
output looks like:
2
u/DonBiggles May 28 '13
Clojure solution (without side effects!):
Output:
2
u/melchyy 0 0 May 28 '13
C Solution
2
u/asthasr May 28 '13 edited May 28 '13
A Scala version:
2
u/[deleted] May 28 '13
First submission. Done in Python. Still trying to work out the display. :)
2
u/Vigenere36 May 28 '13
Done in Java
Output:
M(99)
M(M(110)) since 99 is equal or less than 100
M(100) since 110 is greater than 100
M(M(111)) since 100 is equal or less than 100
M(101) since 111 is greater than 100
91 since 101 is greater than 100
91
2
u/trs717 May 29 '13
Ruby:
Sample Output:
2
u/TweenageDream May 29 '13
I'm loving that the top two are Ruby solutions!
Heres mine:
And my output:
2
u/chilidogg1491 May 29 '13
Solution in Perl:
Output starting with 91:
2
u/tet5uo May 29 '13
Here's my solution in Javascript.
2
u/Edward_H May 29 '13 edited May 29 '13
In F#:
2
u/yoho139 May 29 '13 edited May 29 '13
Got it working in Java.
Got the sample input and output to work as expected. Feedback appreciated, as I'm still learning and it's my first language.
Edit: Fixed the print, I had it displaying the wrong value.
2
u/MrDerk May 29 '13 edited May 29 '13
Quick and dirty python. My print statements aren't 100%, but the net result is there.
Code
Output
EDIT:
Now with 100% accurate print statements:
Output:
There are cleverer ways to handle the print statement in the 'nested' condition, but this was easiest/most readable to me.
2
u/replicaJunction May 29 '13
PowerShell (v3)
2
u/justin1989 May 30 '13
Done in Python:
output:
M(M(110)) since 99 is less than or equal to 100
M(100) since 110 is greater than 100
M(M(111)) since 100 is less than or equal to 100
M(101) since 111 is greater than 100
M(91) since 101 is greater than 100
91
2
u/dude_look May 30 '13 edited May 30 '13
Java
Output
2
u/lemiesz May 30 '13
getting the recursive answer was simple enough. Got tricket by getting the correct output at first, due to the double recursion. My solution to getting the correct output was passing a second variable by reference, this was incremented whenever n was incremented.
C++ https://gist.github.com/anonymous/0a83f05b6d0c3727b0c9
include <stdio.h>
2
u/heschlie May 30 '13
In Perl:
Output:
2
u/Amneamnius May 30 '13
C++
Output:
2
u/[deleted] May 30 '13
Java- feedback welcome
2
u/Tnayoub May 30 '13
My attempt in Java.
2
u/[deleted] May 30 '13
Python:
2
u/Airward May 30 '13
Seems like it is working (Python):
output(99):
and (87 (can someone explain why in wiki it reaches 91 twice?)):
2
u/hatesPonies May 31 '13
Here is my solution in python. Everything should be working. Although I'm fairly satisfied with the recursive solution, I would love some input on getting rid of the 'lastRun' variable. Basically, I would love to find out the step that I'm missing that would make it an elegant solution.
2
u/L8D May 31 '13
My first submission to this subreddit. :D
C
And optionally to activate it through runtime args:
2
u/Idra_rage_lulz Jun 02 '13 edited Jun 02 '13
C++
2
u/PuffGuru Jun 03 '13 edited Jun 03 '13
Java Solution. Always open to feedback. https://gist.github.com/mtjandra/6af03a1cf89169ada37b
2
u/baltoszrhnds Jun 03 '13
A solution in Python:
2
u/CatZeppelin Jul 24 '13 edited Jul 24 '13
Here's my C solution -- I have to admit I've had virtually no sleep, as such it took me a while to grasp the problem, oh well.
Thanks to this comment for showing a more idiomatic approach. I'm only on the 2nd chapter of K&R. Small note: if there's no input, the program will segfault. I should error-check
argv, in the interest of brevity, I did not error check.2
u/missblit Jul 30 '13
Holy fish-sticks that sample output format was a pain. C++
2
u/lets_see_exhibit_A Nov 06 '13
super short recursive java solution:
3
u/montas May 28 '13 edited May 28 '13
My Ruby version
Sample output:
And short version without output
3
u/nakyu May 29 '13
Python3
2
u/Rubicks May 28 '13 edited May 28 '13
Ruby, kind of messy, going to make better.
Edit: Easier to read? Still bad, just trying to think about it whilst doing some other work.
2
u/FatShack May 28 '13 edited May 28 '13
Let's try this in Python
Output:
M(99)
M(M(110)) since 99 is equal to or less than 100
M(100) since 110 is greater than 100
M(M(111)) since 100 is equal to or less than 100
M(101) since 111 is greater than 100
91 since 101 is greater than 100
91
1
u/FourWordUserName 0 1 May 28 '13
D. Just starting to learn the language. Like it so far
1
u/vijaymahes May 29 '13
My attempt in JavaScript:
1
u/Potato2k4 May 29 '13
I got a little lazy with the output, but here is my solution in Python :)
1
u/mcdukex May 29 '13
Scala:
Sample output:
1
u/nanogyth May 29 '13
Python 3
1
u/hammypants May 29 '13
C#, second challenge!
Output:
Enter integer input:
99
M(99)
M(M(89)) since 99 is equal to or less than 100.
M(100) since 110 is greater than 100.
M(M(90)) since 100 is equal to or less than 100.
M(101) since 111 is greater than 100.
M(91) since 101 is greater than 100.
91
1
u/lazynothin May 29 '13
Hey all, first time poster. A co-worker just showed me this subreddit today. Figured I'd take a stab...
I'm a Unix guy. So here's is my shell version of the challenge.
1
u/[deleted] May 29 '13
Python:
def mcCarthy(n):
print mcCarthy(99)
1
u/JBu92_work Jun 05 '13
Here's my solution for the function itself in perl. I didn't do the input/output, but that's the idea around doing a function.
1
u/xevz Jun 09 '13
O'Caml ("stylised" output):
1
u/[deleted] Jun 12 '13 edited Jun 12 '13
recursive implementation in Go, would be glad to hear any kind of critique/advice.
Output:
91 (apparently I'm unable to properly format the output, sorry)
package main
import "fmt"
func mccarthy(n int) int { if n > 100 { fmt.Printf("M(%d) since %d is > 100\n", n-10, n) return n - 10 } else { fmt.Printf("M(M(%d) since %d is <= 100\n", n+11, n) return mccarthy(mccarthy(n + 11)) } }
func main() { fmt.Print("Enter a number: ") var number int fmt.Scanln(&number) fmt.Println(mccarthy(number)) }
1
u/psyomn Jun 14 '13 edited Jun 14 '13
I felt creative. Somewhat.
Edit: Derp. I did not notice this was submitted 16 days ago.
1
u/odinsride Jun 20 '13
My PL/SQL version
1
u/TheCiderman Jun 25 '13
My C++ solution.
The output was
for M(99)
for M(83)
1
u/[deleted] Jun 27 '13
C# Solution
1
u/[deleted] May 28 '13
Python, my first submission here. I do believe I understood the prompt correctly...
1
u/sean0g May 28 '13 edited May 28 '13
Python, still need to figure out the line by line output though.
EDIT: Played with it a little to get the desired output. Comments welcome on a cleaner way.
1
u/huashuai May 29 '13
First time to try dailyprogrammer challenge using Python.
1
u/skyangelisme 0 1 May 29 '13
a bit late to this one, timezones are killing me:
output:
0
u/[deleted] May 29 '13
[deleted] — view deleted comment