| Bytes | Lang | Time | Link |
|---|---|---|---|
| 066 | AWK | 250908T165902Z | xrs |
| 030 | Thunno 2 ṡ | 230620T175550Z | The Thon |
| 036 | 05AB1E | 170201T201819Z | Okx |
| 079 | Noether | 170601T115708Z | Beta Dec |
| 098 | ShortScript | 150902T133455Z | YourDeat |
| 085 | Swift >=2.2 | 220310T014451Z | Bbrk24 |
| 181 | Thue | 220222T045256Z | user1011 |
| 034 | Vyxal ṡ | 220222T224246Z | Seggan |
| 154 | Quipu | 220222T200631Z | DLosc |
| 056 | APL Dyalog Unicode | 150903T042830Z | Adá |
| 064 | J | 180108T044956Z | FrownyFr |
| 082 | Befunge93 | 180108T042633Z | Jo King |
| 030 | 05AB1E | 170601T130317Z | Erik the |
| 071 | PowerShell | 170601T131332Z | colsw |
| 067 | Java | 150830T230947Z | user9023 |
| 074 | Fourier | 150831T090927Z | Beta Dec |
| 084 | SmileBASIC | 170201T203737Z | 12Me21 |
| 078 | Python 3 | 160211T134832Z | Ogaday |
| 9079 | Julia | 160103T024848Z | M L |
| 074 | Perl 6 | 160115T183135Z | Brad Gil |
| 068 | beeswax | 160103T021638Z | M L |
| 073 | Mouse2002 | 160103T191334Z | cat |
| 271 | Rust | 150903T192040Z | XAMPPRoc |
| 085 | PowerShell | 150831T205529Z | AdmBorkB |
| 076 | Python 2 | 150830T225459Z | TheNumbe |
| 087 | Swift 2.0 | 150902T141308Z | GoatInTh |
| 084 | JavaScript | 150902T073225Z | curiousd |
| 107 | IA32 machine code + linux | 150901T193923Z | anatolyg |
| 086 | Bash | 150831T151027Z | pawel.bo |
| 088 | PHP | 150901T163109Z | Benoit E |
| 063 | Perl | 150831T205258Z | ThisSuit |
| 095 | C# 6 | 150831T064121Z | user3094 |
| 100 | PHP | 150831T171540Z | ale8oneb |
| 092 | JavaScript | 150831T173502Z | Tiago Si |
| 088 | Haskell | 150831T070328Z | Otomo |
| 129 | Fortran | 150901T052253Z | edc65 |
| 092 | JavaScript | 150831T092427Z | user4180 |
| 076 | Bash | 150901T042958Z | Digital |
| 078 | Python 2 | 150901T041058Z | mbomb007 |
| 118 | Lua | 150901T015404Z | Nikolai9 |
| 087 | Haskell | 150901T015016Z | lynn |
| 091 | PHP 7 | 150831T200507Z | Fabian S |
| 068 | Ruby 1.9+ | 150831T111634Z | Ibrahim |
| 092 | Common Lisp | 150831T093611Z | coredump |
| 105 | MATLAB | 150831T085825Z | Stewie G |
| 092 | Processing | 150831T115442Z | user4180 |
| 109 | Inform 7 | 150831T135642Z | curiousd |
| 083 | C | 150831T024915Z | Mauren |
| 047 | CJam | 150830T224502Z | aditsu q |
| 092 | Swift | 150831T113521Z | Alexande |
| 061 | GolfScript | 150831T111416Z | user3094 |
| 109 | C++ | 150831T045333Z | EvgeniyZ |
| 094 | SWIProlog | 150831T062906Z | Fatalize |
| 066 | Julia | 150830T230107Z | Alex A. |
| 049 | Pyth | 150831T021950Z | orlp |
| 067 | O | 150831T011648Z | kirbyfan |
| 080 | R | 150830T235427Z | flodel |
| 053 | Pyth | 150831T000449Z | kirbyfan |
| 105 | rs | 150831T002609Z | kirbyfan |
| 149 | Labyrinth | 150830T230824Z | Martin E |
| 079 | Mouse | 150830T231101Z | Alex A. |
| 066 | JavaScript ES6 | 150830T225918Z | jrich |
AWK, 66 bytes
$0=$1" is "($1-$2?$1>$2?"greater than":"less than":"equal to")FS$2
Thunno 2 ṡ, 30 bytes
ṛ‘~µ to%ɱṚ ċç%ƁÞ ċç‘'%/i¹s`is$
Only works in \$\le 2.2.0\$. ATO is currently on v\$2.1.9\$.
To make this work in all versions use -ʂ instead of ṛ.
Explanation
ṛ‘...‘'%/i¹s`is$ '# Implicit input
ṛ # Compare the two inputs. Push -1 if a>b, 0 if a==b, 1 if a<b.
‘...‘ # Push compressed string "equal to%less than%greater than"
'%/ '# Split it on "%"s to get ["equal to","less than","greater than"]
i # Index the number from before into this list
¹s # Push the second input and swap
`is # Push the string "is"
$ # And push the first input
# Implicit output of the stack joined by spaces
05AB1E, 38 36 bytes
²¹.S>ð“îØ€ë
ž‰€„
†ª€ë“«¶¡è¹… is«sð²J
I'm surprised there isn't already a 05AB1E answer. You can write almost any word in 05AB1E with just 2 bytes.
Noether, 91 79 bytes
I~aP" is "PI~b{ab=}{"equal to"P}{ab>}{"greater than"P}{ab<}{"less than"P}" "PbP
Note that while Noether is based heavily on Fourier, I have sacrificed golfability for functionality.
ShortScript, 98 bytes
←Α
←Β
↑Γαis
↔α>β→γgreater thanβ
↔α<β→γless thanβ
↔α|β→γequal toβ
Swift >=2.2, 85 bytes
func f(a:Int,b:Int){print(a,"is",a==b ?"equal to":(a>b ?"greater":"less")+" than",b)}
The two current Swift answers, both in Swift 2.0, are invalid because they print the literal letters "A" and "B" instead of the values given into the function. Other than that, this is pretty much identical to those two.
I can't find any Swift 2.0 or 2.1 compilers online, so I can't say for certain it works in those versions, but I don't see why it wouldn't. The function is f(_:b:) in Swift 2.2 and f(a:b:) in Swift >=3.0.
Thue, 181 bytes
1,1::=o,x
x1::=1x
x]::=]x
!x::=!o
11,]::=1o,]
[1,]::=!vo
vx::=>x
[,11::=[,1x
[,1]::=!<x
[,]::=!=
o::=~1
>::=~ is greater than
<::=~ is less than
=::=~ is equal to
*::=:::
::=
[*]
The TIO version contains a trailing newline, but that’s only needed in this specific implementation; the specification doesn’t specify that the trailing newline is needed and other interpreters (e.g.) don’t require it. Similarly, the newlines in the output don’t show up in the linked interpreter.
IO Format
Input and output are in unary strings of 1s, and there is a comma (but no spaces) between A and B. e.g. For A = 2 and B = 3, input 11,111.
Explanation (not really)
Unfortunately, my own motivation to write an explanation is inversely proportional to the reader’s desire for one, so I won’t write a detailed explanation. However, this ungolfed version that’s just split into sections might help (I’ll use # to denote comments):
# General stuff
1,1::=o,x
x1::=1x
x]::=]x
!x::=!o
# Check for greater than-ness
11,]::=1o,]
[1,]::=!vo
vx::=>x
# Check for less than-ness
[,11::=[,1x
[,1]::=!<x
# Check for equality
[,]::=!=
# Output
o::=~1
>::=~ is greater than
<::=~ is less than
=::=~ is equal to
# Input
*::=:::
::=
[*]
Look at the Input, Output, and General stuff sections first before looking at the other sections.
Also, maybe use this interpreter to step through some examples.
Original answer:
Thue, 48 bytes, non-competing due to output format
*::=:::
1_1::=_
1__::=~>
__1::=~<
___::=~=
::=
*
IO format
The numbers are inputted in unary as strings of 1s, with an underscore on either side and an underscore between them. For example, to compare 2 and 3, the input would be _11_111_.
If the input is _A_B_ then the output is > if \$A>B\$, < if \$A<B\$, and = if \$A=B\$.
Explanation
I will assume some familiarity with Thue, so go read the Language description section of the Esolang page.
*::=::: Take input (see also the last two lines).
1_1::=_ (Continually) subtract 1 from both numbers while both are positive.
1__::=~> If B becomes 0 but A remains positive, A > B.
__1::=~< If A becomes 0 but B remains positive, A < B.
___::=~= If both A and B become 0, A = B.
::=
*
Vyxal ṡ, 34 bytes
=[«ß‟₆nḭ«|<[`∨⅛`|`∆ḭ`]`λ⊍`]?^‛is?^
=[«ß‟₆nḭ«|<[`∨⅛`|`∆ḭ`]`λ⊍`]?^‛is?^ # Takes 2 numbers as arguments
=[«ß‟₆nḭ« ] # If equal, push the base compressed string "equal to"
| # Otherwise...
<[`∨⅛` ] # If less than, push the string "less"
|`∆ḭ` # Otherwise, push the string "greater"
`λ⊍` # Push the string "than"
? # Push the first number (due to the input mode, this turns out to be the second input)
^ # Reverse the stack so we can prepend stuff
‛is # Push the string "is"
? # Push the second number (first input)
^ # Reverse the stack again
# Implicit output. ṡ joins the stack on spaces
Quipu, 154 bytes
'g'l'e\/' 3&5&2&[]1&' 4&
'r'e'q/\'i[][][]/\[]/\[]
'e's'u 's4&9&/\1@/\ /\
'a's'a ' []<<1@??
't' 'l /\--8&??
'e't' \/ >>
'r'h't
' 'a'o
't'n
'h
'a
'n
Explanation
Threads 0 through 2 store the strings greater than, less than, and equal to, respectively. Then:
- Thread 3 inputs a number, stores it, and also outputs it
- Thread 4 outputs
is, then inputs a number and stores it - Thread 5 calculates the value of thread 3 minus the value of thread 4
- Thread 6 loads the value of thread 5 and:
- jumps to thread 9 if it is less than 0
- jumps to thread 8 if it is greater than 0
- continues to thread 7 otherwise
- Thread 7 loads the value of thread 2, outputs it, and jumps to thread 10
- Thread 8 loads the value of thread 0, outputs it, and jumps to thread 10
- Thread 9 loads the value of thread 1, outputs it, and continues to thread 10
- Thread 10 outputs a space
- Thread 11 loads the value of thread 4 and outputs it
APL (Dyalog Unicode), 56 bytesSBCS
Anonymous infix lambda.
{∊⍺'is ','less than' 'equal to' 'greater than'[2+×⍺-⍵]⍵}
{…} "dfn"; left argument is ⍺ and right argument is ⍵:
⍵ the right argument to the right of
'less than' 'equal to' 'greater than'[…] this list of three strings indexed by:
⍺-⍵ left argument minus right argument
× sign of that
2+ add two to that
⍺'is ', prepend the left argument and the string "is "
∊ ϵnlist (flatten)
J, 64 bytes
9;:inv@A.;&":,'is'~.@;cut@'equal to greater than less'{~1 2*>-<
equal to is joined by a non-break space.
1 2*>-<: –1, –2 if less; 0, 0 if equal; 1, 2 if greater than
;&":,'is'~.@; prepend is, then prepend A and B. Because 0, 0 yields equal to,equal to, there’s a ~. to remove duplicates.
9 A. to shuffle B to the last position.
;:inv@ join by spaces
Befunge-93, 82 bytes
<v"equal to "$_v#!\!`0:\0:-&,,,"is ".::&
v "less than "_" naht retaerg"
@>:#,_$-.
PowerShell, 71 Bytes
param($a,$b)"$a is $($a-$b|% T*g "greater than;less than;equal to") $b"
an update to the existing powershell answer from 2 years ago.
Java, 114 113 Bytes or 74 72 67 if we used lambda notation
Thanks to Kevin Cruijssen for currying based solution:
a->b->a+" is "+(a==b?"equal to ":(a>b?"greater":"less")+" than ")+b
Old pre lambda solution
public void c(int a,int b){System.out.print(a+" is "+(a==b?"equal to ":(a>b?"greater":"less")+" than ")+b);}
as user h.j.k meantion in comment, if we used lambda we can do significantly down to 74 bytes.
(a,b)->a+" is "+(a==b?"equal to ":(a>b?"greater":"less")+" than ")+b;
Fourier, 147 74 bytes
Non-competing because string printing is newer than this challenge
I~AoI~B` is `<A{1}{`greater than`}A<B{1}{`less than`}A{B}{`equal to`}` `Bo
Dunno why I didn't allow printing before... It makes the code readable and is great for golfing
SmileBASIC, 84 bytes
INPUT A,B
S=B-A?A;" is ";MID$("greater equal toless ",SGN(S)*8+8,8);"than"*!!S;" ";B
Python 3, 78 bytes
Translation of TheNumberOne's, using a double conditional instead of cmp, doesn't do too badly imo.
lambda a,b:print(a,'is',[['equal to','greater than'][a>b],'less than'][a<b],b)
You can also replace cmp(a,b) with (a>b)-(a<b), but when I replaced it directly I found it one byte longer than my solution above. I also tried returning a string, instead of printing, but that was longer too.
Julia, 90 79 chars
Arrays in Julia use 1-based indexing, so adding 2 to the cmp function is necessary for proper access to the array.
c(a,b)=print(a," is ",["less than ","equal to ","greater than "][cmp(a,b)+2],b)
Result:
julia> c(2,2)
2 is equal to 2
julia> c(1654654,0)
1654654 is greater than 0
julia> c(-99999999999,-654654654)
-99999999999 is less than -654654654
Perl 6, 74 bytes
->\a,\b{say a~" is {<less equal greater>[1+(a <=>b)],<than to>[a==b]} "~b} # 74
Usage:
# give it a lexical name for ease of use
my &compare = ->\a,\b{ … }
compare 1,2;
compare 2,2;
compare 3,2;
say '';
compare "{ 2²⁵⁶ }\n", "\n{ 2²⁵⁶ + 1 }"
1 is less than 2
2 is equal to 2
3 is greater than 2
115792089237316195423570985008687907853269984665640564039457584007913129639936
is less than
115792089237316195423570985008687907853269984665640564039457584007913129639937
beeswax, 68 chars
Non-competing answer, as I finished creating beeswax in December 2015, long after the start of the challenge.
The natural number range of beeswax is 64 bit unsigned integers, so this should be a valid solution. I’ll post a solution for 64 bit signed integers soon.
_T~T~{` is `Kp`equal to `{
p`ht ssel`#L~EL#~`greater than `{
>`an `{
You can clone the full and latest version of my beeswax interpreter, including examples and language specification from my GitHub repo.
Mouse-2002, 73 bytes
Uses variables (lame, I know).
?a:?b:"A is "a.b.>["greater than"]a.b.<["less than"]a.b.=["equal to"]" B"
Explained:
? a: ? b: ~ store input in a , b
"A is " ~ print
a. b. > [ ~ testif
"greater than" ~ print
]
a. b. < [ ~ rinse && repeat
"less than"
]
a. b. = [
"equal to"
]
" B"
Pure stack version: 96 95 bytes
I had fun writing this one. It would be a LOT shorter if Mouse had a 2dup instruction, but I implemented it myself instead.
??#D;#D;"A is ">["greater than"]<["less than"]=["equal to"]" B"
$D&swap &dup &rot &rot &over @
How this works:
? ? #D; #D; ~ get some input, then 2dup twice
"A is " ~ print this
> [ ~ test if gtr
"greater than" ~ print this
] ~ fallthrough
< [ ~ repeat for next two conditions
"less than"
]
= [
"equal to"
]
" B"
$D ~ def D():
&swap ~ ( x y -- y x )
&dup ~ ( y x -- y x x )
&rot ~ ( y x x -- x x y )
&rot ~ ( x x y -- x y x )
&over ~ ( x y x -- x y x y )
@
We need the 2dup instruction because comparison pops.
Stack operations FTW!
Edit: for the same stack effect but a byte less, the second swap in the 2dup implementation was changed to rot.
Rust, 271 bytes
use std::cmp::Ordering::*;fn main(){let v:Vec<String>=std::env::args().collect();let a:u8=v[1].parse().unwrap();let b:u8=v[2].parse().unwrap();print!("{} is ",a);print!("{}", match a.cmp(&b) {Less=>"less than",Equal=>"equal to",Greater=>"greater than"});print!(" {}",b);}
Pretty printed version:
use std::cmp::Ordering::*;
fn main() {
let v:Vec<String> = std::env::args().collect();
let a:u8 = v[1].parse().unwrap();
let b:u8 = v[2].parse().unwrap();
print!("{} is ", a);
print!("{}", match a.cmp(&b) {
Less => "less than",
Equal => "equal to",
Greater => "greater than"
});
print!(" {}", b);
}
PowerShell, 98 85 Bytes
(Thanks to tomkandy for the huge assist on the param and reminding me that PowerShell likes inline subexpression parsing, so go ahead and use it)
param($a,$b);"$a is $(@("equal to","greater than","less than")[$a.CompareTo($b)]) $b"
Essentially the same, just trimmed down quite a bit.
(Original below)
Decently competitive, I was somewhat surprised. Again, the $ character and taking input is the downfall.
$a=$args[0];$b=$args[1];"$a is "+@("equal to","greater than","less than")[$a.CompareTo(+$b)]+" $b"
(Creating a function of two arguments is one byte longer than taking command-line input of two arguments, thanks to the } at the end)
function r{param($a,$b);<doing_stuff>}
$a=$args[0];$b=$args[1];<doing_stuff>
Expanded:
$a=$args[0]
$b=$args[1]
"$a is " + @("equal to","greater than","less than")[$a.CompareTo(+$b)] + " $b"
Similar in concept to some other languages present, this is utilizing a feature of x.CompareTo(y), which returns -1 if x<y, 0 if x=y, and 1 if x>y. We take that result and pull out the appropriate value from a dynamic array we just created, leveraging the fact that [-1] stands for the last item in the array.
Do note the + inside the CompareTo(+$b) statement -- an explicit cast to an Integer. If we left that off, it would try to utilize the String comparison instead, which yields odd behavior.
Python 2, 95 94 76 bytes
Input must be comma separated.
A,B=input();print A,'is',['equal to','greater than','less than'][cmp(A,B)],B
Swift 2.0, 87 bytes
func g(a:Int,b:Int){print("A is",a==b ?"equal to":a<b ?"less than":"greater than","B")}
JavaScript, 84 bytes
f=function(a,b){alert(a+' is '+(a-b?(a>b?'greater':'less')+' than ':'equal to ')+b)}
IA-32 machine code + linux, 107 bytes
Hexdump of the code:
60 89 e5 89 d0 e8 51 00 00 00 4c c6 04 24 20 38
d1 74 20 68 74 68 61 6e 4c c6 04 24 20 72 0d 68
61 74 65 72 68 20 67 72 65 44 eb 11 68 6c 65 73
73 eb 0a 68 6c 20 74 6f 68 65 71 75 61 68 20 69
73 20 88 c8 e8 12 00 00 00 89 ea 29 e2 89 e1 31
db 43 8d 43 03 cd 80 89 ec 61 c3 5b d4 0a 4c 04
30 88 04 24 c1 e8 08 75 f3 ff e3
Because of hardware limitations, the code works with numbers in the range 0...255.
Source code (can be assembled with gcc):
.globl print_it
.text
.align 16
print_it:
pushal;
mov %esp, %ebp; // save esp (stack pointer)
mov %edx, %eax; // put second number in al
call prepend; // convert al to string
dec %esp; // write ...
movb $' ', (%esp); // ... a space
cmp %dl, %cl; // compare the numbers
je equal; // if equal, goto there
push $0x6e616874; // write "than"
dec %esp; // write ...
movb $' ', (%esp); // ... a space
jb less; // if below, goto there
greater:
push $0x72657461; // write "ater"
push $0x65726720; // write " gre"
inc %esp; // remove a space
jmp finish; // bypass the code for "less than"
less:
push $0x7373656c; // write "less"
jmp finish; // bypass the code for "equal"
equal:
push $0x6f74206c; // write "l to"
push $0x61757165; // write "equa"
finish:
push $0x20736920; // write " is "
mov %cl, %al; // put first number in al
call prepend; // convert al to string
mov %ebp, %edx; // calculate the length ...
sub %esp, %edx; // ... of the output message
mov %esp, %ecx; // address of the message
xor %ebx, %ebx; // set ebx to ...
inc %ebx; // ... 1 (i.e. stdout)
lea 3(%ebx), %eax; // set eax=4 (syscall "write")
int $0x80; // do the system call
mov %ebp, %esp; // restore the stack pointer
popal; // restore other registers
ret; // return
prepend: // writes al converted to string
pop %ebx; // remove return address from the stack
appendloop:
aam; // calculate a digit in al, rest in ah
dec %esp;
add $'0', %al; // convert the digit to ASCII
mov %al, (%esp);// write the digit
shr $8, %eax; // replace al by ah; check if zero
jnz appendloop; // nonzero? repeat
jmp *%ebx; // return
This is some serious abuse of the stack! The code builds the output message on the stack, from the end to the beginning. To write 4 bytes, it uses a single push instruction. To write 1 byte, it uses two instructions:
dec %esp
mov %al, (%esp);
By luck, most of the fragments to write are 4 bytes. One of them ("gre" in "greater") is 3 bytes; it's handled by pushing 4 bytes and removing one afterwards:
inc %esp
The routine that writes numbers in decimal form uses the aam instruction to divide ax by 10 repeatedly. It's advantageous that it calculates the digits from right to left!
Since there are two numbers to write, the code uses a subroutine, which is called twice. However, because the subroutine writes the results on the stack, it uses a register to hold the return address.
C code that calls the machine code above:
include <stdio.h>
void print_it(int, int) __attribute__((fastcall));
int main()
{
print_it(90, 102);
puts("");
print_it(78, 0);
puts("");
print_it(222, 222);
puts("");
return 0;
}
Output:
90 is less than 102
78 is greater than 0
222 is equal to 222
Bash, 94 86 bytes (saved eight bytes thanks to Digital Trauma)
p=equal;q=than;(($1>$2))&&p=greater&&[ ]||(($1<$2))&&p=less||q=to;echo $1 is $p $q $2
Test (on Linux):
echo 'p=equal;q=than;(($1>$2))&&p=greater&&[ ]||(($1<$2))&&p=less||q=to;echo $1 is $p $q $2' > cmp.sh
chmod +x cmp.sh
./cmp.sh 10 12
10 is less than 12
The use of [ ] after p=greater is to prevent || operator from being evaluated before = in the expression ...&&p=greater||(($1<$2))... (the operator precedence!).
The alternative would be using brackets around (($1>$2))&&p=greater and (($1<$2))&&p=less , but brackets make inner scope for variables, so p would be left unaltered.
PHP, 88 bytes
PHP function, 88 bytes:
function a($a,$b){echo"$a is ",$a-$b?($a>$b?"greater":"less")." than":"equal to"," $b";}
function b($a,$b){echo"$a is ",$a-$b?$a>$b?"greater than":"less than":"equal to"," $b";}
function c($a,$b){echo"$a is ",$a-$b?($a>$b?"greater":"less")." than $b":"equal to $b";}
Note than $a-$b can be replaced by $a^$b.
Full program, 89 bytes:
<?=$a=$argv[1]," is ",$a-($b=$argv[2])?($a>$b?"greater":"less")." than $b":"equal to $b";
I used:
- String interpolation.
- Nested ternary.
- Some echo tricks, like using commas instead of concatenating or using PHP short echo tag.
Perl, 64 63 bytes
#!/usr/bin/perl -p
s/ /" is ".("equal to ",greaterx,lessx)[$`<=>$']/e;s/x/ than /
62 bytes + 1 byte for -p. Takes input from STDIN, with the two numbers separated by a single space:
$ echo 1 2 | ./cmp
1 is less than 2
$ echo 42 -17 | ./cmp
42 is greater than -17
$ echo 123456789 123456789 | ./cmp
123456789 is equal to 123456789
How it works:
The <=> operator returns -1, 0, or 1 depending on whether the first operand is less than, equal to, or greater than the second. Conveniently, Perl allows negative subscripts with arrays and slices, where the last element is at position -1, the second-to-last element is at position -2, and so on.
In the code
("equal to ",greaterx,lessx)[$`<=>$']
we use the return value of <=> as the subscript in a list slice to get the corresponding string, where $` is the first number and $' is the second.
To avoid repeating than, x is used as a placeholder and replaced in a second substitution at the end.
Alternative solution, 63 bytes
#!/usr/bin/perl -p
@a=(equal,greater,than,to,less);s/ / is @a[$i=$`<=>$',!$i+2] /
62 bytes + 1 byte for -p. Takes space-separated input from STDIN just like the first solution.
How it works:
This solution also uses a slice, but takes advantage of the fact that unlike list slices, array slices can be interpolated into strings (and the RHS of substitutions). This lets us drop the /e modifier and the quotes in the substitution operator.
The real trick is in the slice subscript:
@a[$i=$`<=>$',!$i+2]
For the different values of <=>, this gives:
$i !$i+2 $a[$i] $a[!$i+2]
----------------------------
-1 2 less than
0 3 equal to
1 2 greater than
When an array or array slice is interpolated into a string, the elements are automatically joined by $" (by default, a single space).
C# 6, 113 103 100 95 bytes
void C(int a,int b){System.Console.Write($"{a} is {a-b:greater than;less than;equal to} {b}");}
Thanks to edc65 for saving 13 bytes and to cell001uk for saving 5 bytes using C# 6's interpolated strings!
PHP, 103 100 bytes
103 byte version.
function c($a,$b){echo "A is ".($a==$b?"equal to":($a>$b?"greater than":($a<$b?"less than":"")))." B";}
100 byte version (thanks jrenk)
<?php function c($i,$u){echo$i.' is '.($i>$u?'greater than ':($i==$u?'equal to ':'less than ')).$u;}
JavaScript, 92 bytes
a=+prompt(),b=+prompt();alert(a+" is "+(a>b?"greater than ":a<b?"less than ":"equal to ")+b)
Haskell, 97 94 88 bytes
a!b|a>b="greater than "|a<b="less than "|1<3="equal to "
a#b=show a++" is "++a!b++show b
thanks to nimi
94 byte version:
d a|a>0="greater than "|a<0="less than "|1<3="equal to "
c a b=show a++" is "++d(a-b)++show b
97 byte version:
d a b|a>b=" greater than "|a<b=" less than "|1<3=" equal to "
c a b=show a++" is"++d a b++show b
my first time golfing :) (tested with ghci, size determined with du and stat)
Fortran, 129
Fortran arithmetic if is perfect for this challenge
Test: ideone
read(*,*)i,j
if(i-j)1,2,3
1 print*,i," is less than",j
stop
2 print*,j," is equal to",j
stop
3 print*,i," is greater than",j
end
JavaScript, 151 104 100 95 92 bytes
a+=prompt()
b+=prompt()
alert(a+" is "+(a>b?"greater than ":a<b?"lesser than ":"equal to ")+b)
I managed to shorten with help of edc65
Bash, 76
a=(less\ than equal\ to greater\ than)
echo $1 is ${a[($1>$2)-($1<$2)+1]} $2
Python 2, 78 bytes
I love how cmp() is really useful, but it was removed in Python 3.
Using an anonymous function:
lambda a,b:`a`+' is '+['equal to ','greater than ','less than '][cmp(a,b)]+`b`
Not using a function (79 bytes):
a,b=input();print a,'is %s'%['equal to','greater than','less than'][cmp(a,b)],b
Lua, 118 bytes
I don't see enough Lua answers here so...
function f(a,b)print(a>b and a.." is greater than "..b or a==b and a.." is equal to "..b or a.." is less than "..b)end
Ungolfed:
function f(a,b)
print(a>b and a.." is greater than "..b or a==b and a.." is equal to "..b or a.." is less than "..b)
end
Haskell, 87 bytes
One byte shorter than Otomo's approach.
a?b=show a++" is "++["less than ","equal to ","greater than "]!!(1+signum(a-b))++show b
PHP 7, 91 bytes
<?=$a=$argv[1],[~ß–ŒßšŽŠž“ß‹ß,~ß–Œß“šŒŒß‹—ž‘ß][($b=$argv[2])<=>$a]?:~ß–Œß˜šž‹šß‹—ž‘ß,$b;
Usage:
Save as ANSI in file.php and run:
php -derror_reporting=~E_NOTICE file.php A B
Is suppressing notices via CLI flag considered cheating? If so, here is a version that works with full error reporting, (93 Bytes)
<?=$a=$argv[1],@[~ß–ŒßšŽŠž“ß‹ß,~ß–Œß“šŒŒß‹—ž‘ß][($b=$argv[2])<=>$a]?:" is greater than ",$b;
Some Explanation
For the string constants, I used undefined constants and bitwise invert them with ~. PHP replaces undefined constants with the name of the constant (foo => "foo") and allows special characters in constant names. This way you can save a byte even for strings with whitespace. Example: ~ß => " ")
The spaceship operator <=> returns 0 if the operands are equal, -1 if the left operand is less than the right operand and 1 if the left operand is greater than the left operand. I use this as array index, to get " is equal to " and " is less than ". For the non-existing array key -1 the result is null and thanks to ?: we get " is greater than " in this case.
Ruby 1.9+, 73 71 70 68 bytes
->a,b{[a,b]*" is #{%w{equal\ to greater\ than less\ than}[a<=>b]} "}
Common Lisp, 92 bytes
(lambda(a b)(format t"~A is ~[less than~;equal to~;greater than~] ~A"a(1+(signum(- a b)))b))
More readably:
(lambda(a b)
(format t "~A is ~[less than~;equal to~;greater than~] ~A"
a ; first ~A
(1+(signum(- a b))) ; gives 0, 1 or 2 which selects a clause in ~[ ~]
b ; second ~A
))
MATLAB, 105 bytes
x=input('');y=input('');t={'less than','greater than','equal to'};
sprintf('%i is %s %i',x,t{(x>=y)+(x==y)+1},y)
Added a line break before sprintf, to ease readability. It works both with and without this line break, so it's not included in the byte count. Must hit enter between the two input numbers.
Processing, 92 bytes
void c(int a,int b){print(a+" is "+(a>b?"greater than ":a<b?"lesser than ":"equal to ")+b);}
Inform 7, 109 bytes
To(A - value)T(B - value):say "[A] is [if A is B]equal to [else if A > B]greater than [else]less than ";say B
This defines a phrase used as 1 T 2;.
C, 155 136 127 83 bytes
f(a,b){printf("%d is %s %d\n",a,a>b?"greater than":a<b?"less than":"equal to",b);}
CJam, 47
q~_~-g"is
equal greater less
to than"N/Sf/f=*S*
Explanation:
q~ read and evaluate the input (array of 2 numbers)
_ duplicate the array
~- dump one array on the stack and subtract the numbers
g get signum (-1 for <, 0 for ==, 1 for >)
"…" push that string
N/ split into lines
Sf/ split each line by space
f= get the corresponding word (for the signum) from each line
* join the array of 2 numbers by the array of words
it effectively inserts the words between the numbers
S* join everything with spaces
Swift, 105 92 byte
func c(a:Int, b:Int){println("A is",(a==b ?"equal to":(a<b ?"less":"greater")," than"),"B")}
even shorter with Swift 2.0 (103 90 byte)
func c(a:Int, b:Int){print("A is",(a==b ?"equal to":(a<b ?"less":"greater")," than"),"B")}
GolfScript, 61 bytes
\.@.@="equal to "{.@.@>"greater""less"if" than "+}if" is "\+@
Expects 2 integers on the stack. Try it online.
How it works:
\.@.@- A and B are already on the stack, and this code piece makes the stack look like this:ABBA.\swaps the two top items on the stack,.duplicates the top item, and@rotates the 3 top items (1 2 3->2 3 1).Then, three items are pushed to the stack: the
=sign,"equal to ", and the block between{}. Theifstatement does this: if the first argument evaluates to true, it executes the first code block (the second argument), otherwise, the second code block (the third argument). So if A and B are equal, it will push "equal to " on the stack. If they are not equal, it will execute the code between the block. Note that=pops the two top items from the stack, so now the stack looks likeAB.Inside the block, you first see
.@.@. Before these commands, the stack looks likeAB, and after, the stack looks likeBAAB. The commands are similar as the ones mentioned above.Then, there's another
ifstatement. This time, it checks whether A > B, and if true, it pushes "greater" on the stack. Else, it pushes "less" on the stack. After pushing one of these two, it will push " than " on the stack and concatenate it with the previous pushed string.>also pops the two top items of the stack, so now the stack looks likeBA"string".The next three commands are:
" is "\+." is "pushes that string on the stack (stack looks likeBA"string"" is "),\swaps the two top items (stack looks likeBA" is ""string"), and+concatenates the two top items (stack looks likeBA" is string").The last command,
@, rotates the three stack items, so the stack now looks like:A" is string"B. GolfScript automatically prints the stack values on STDOUT once the program terminates, so then you get the desired output.
C++, 109 bytes
#include<cstdio>
int x(int a,int b){printf("%d is %s %d",a,a>b?"greater than":a<b?"less than":"equal to",b);}
SWI-Prolog, 94 bytes
a(A,B):-(((A>B,C=greater;A<B,C=less),D=than);C=equal,D=to),writef("%t is %t %t %t",[A,C,D,B]).
Julia, 69 66 bytes
f(A,B)="$A is $(A>B?"greater than":A<B?"less than":"equal to") $B"
This uses string interpolation to embed A, B, and the ternary inside a single string.
Saved 3 bytes thanks to Glen O.
Pyth, 52 49 bytes
jdm@cd)._-FQcj"
is
equal greater less
to than
"Qb
R, 80 bytes
function(A,B)cat(A,"is",c("less than","equal to","greater than")[2+sign(A-B)],B)
Pyth, 57 55 53 bytes
AQjd[G"is"@c"equal to
greater than
less than"b._-GHH)
This basically does:
["less than", "greater than", "equal to"][sign_of(A-B)]
Saved 2 bytes thanks to @AlexA.'s suggestion of using A instead of J and K and another 2 bytes by replacing the whole addition mess with a simpler subtraction.
55-byte version
AQjd[G"is"@c"less than
greater than
equal to"b+gGHqGHH)
57-byte version:
jd[JhQ"is"@c"less than
greater than
equal to"b+gJKeQqJKK)
rs, 105 bytes
(\d+) (\d+)/\1 is (_)^^(\1) (_)^^(\2) \2
(_+) \1_+/ less than
(_+) \1 / equal to
_+ _+/greater than
The trailing and preceding whitespace is very important!
Labyrinth, 180 152 149 bytes
<
?01.23.511.501.23};,!:?
:
= ;3.114.101.97.116.101.114.32.116.104.97.110.32.{!@
-""
; ;8.101.115:..""""""""""""^
1
.113.117.97.108.32.116.111.32.{!@
Edit: Managed to shave off 3 bytes by reusing 10 between 101, 103 and 108 (the character codes of e, g and l). The explanation below does not reflect this, but it's not a substantial change.
Explanation
There isn't much we can do in the way of saving bytes for printing the strings, that's just going to be long linear sections. So the main challenge in golfing is to avoid large amounts of unnecessary whitespace. That means we want the linear parts to "radiate out" from the left-most column. We can also gain some more savings by reusing the code that prints than B. So let's look at the control flow here:
The program starts on a grid rotation command <. This shifts the current row cyclically to the left with the IP on it, so we get this:
<
?.23.511.501.23};,!:?
:
= ;103.114.101.97.116.101.114.32.116.104.97.110.32.{!@
-""
1 ;108.101.115:..""""""""""""^
0
1.113.117.97.108.32.116.111.32.{!@
Now the IP is on an isolated cell, so it executes the same command again and again while the < travels further to the left until...
<
?.23.511.501.23};,!:?
:
= ;103.114.101.97.116.101.114.32.116.104.97.110.32.{!@
-""
1 ;108.101.115:..""""""""""""^
0
1.113.117.97.108.32.116.111.32.{!@
At this point, the IP has somewhere to go and executes the first linear section (the second row) from right to left. What it does is read A, copy, print. Consume the delimiting character between the numbers, print is (and spaces). Then read B, copy it and subtract A from it at the -.
At this point we hit first "fork in the road". If the difference yielded 0, the IP keeps moving straight ahead towards the bottom branch. That branch simply prints equal to and then B.
Otherwise, the IP takes a left towards the two no-ops "". Then there's another fork. If the difference was negative, the IP takes another left towards the long upper branch. That branch simply prints greater than and then B.
If the difference was positive, the IP takes a right onto the lower branch, which prints less. Now we want to reuse the than from the other branch. But at the same time we don't want to connect the two branches later on, because we'd need a whole bunch of unnecessary spaces. Instead we use a few no-ops to align the lower branch with where the than begins on the upper branch and then start manipulating the source again with ^:
<
?.23.511.501.23};,!:?
: .
= ;103.114.101.97.116.101.114 32.116.104.97.110.32.{!@
-"" ^
1 ;108.101.115:..""""""""""""
0 2
1.113.117.97.108.32.116.111.3 .{!@
Again, this is isolates the IP, so ^ is executed again and we get
<
?.23.511.501.23};,!:? .
:
= ;103.114.101.97.116.101.114^32.116.104.97.110.32.{!@
-""
1 ;108.101.115:..""""""""""""2
0
1.113.117.97.108.32.116.111.3 .{!@
Now the IP can continue moving to the right and print than and B as required.
Mouse, 79 bytes
?A:?B:A.!" is "A.B.<["less than"]A.B.>["greater than"]A.B.=["equal to"]" "B.!$
When strings are encountered they're immediately written to STDOUT rather than being put on the stack. The stack can contain only integers.
Ungolfed:
? A: ~ Read an integer A from STDIN
? B: ~ Read an integer B from STDIN
A. ! ~ Write A to STDOUT
" is "
A. B. < [ "less than" ] ~ If A < B
A. B. > [ "greater than" ] ~ If A > B
A. B. = [ "equal to" ] ~ If A == B
" "
B. ! ~ Write B to STDOUT
$ ~ End of program
JavaScript (ES6), 66 bytes
(a,b)=>a+` is ${a<b?"less than":a>b?"greater than":"equal to"} `+b
Defines an anonymous function.
Test by adding f= before it, and call it like alert(f(4, 5))
No savings to be had from the repetitive "than", unfortunately.