Notcalc(間違った答えを計算する)


33

ゴール:

2つの自然数(0から無限大までの整数)が与えられた場合、それらの数の合計ではなく自然数である数値を出力します。

ソリューション例(TI-Basic):

  • A+B+1

  • not(A+B)

無効なソリューション:

  • A+B-1(入力の0,0場合、それは戻ります-1、これは自然ではありません)

  • "ABC"ABC数字ではありません)

ノート:

  • 出力は常に2つの自然数の合計でなければなりません(実際には単なる自然数です)

  • -1undefinedinfinityNaNおよびエラーメッセージは自然数ではありません。私たちの目的で0は、これは自然です(ただし、すべての数学者が同意するわけではありません)。


1
多分、数字を文字列として、文字列として出力しますか?
XNOR

1
出力の先頭にゼロを付けることはできますか?
クリチキシリトス

1
オーバーフローを考慮する必要があると思うので、2 ^ 32 -1と2の結果は負であってはいけませんよね?
adrianmp

1
私は無駄な詳細に注意を払うのが好きなので、ちょっとした発言:0は自然数ではありません。最初の文を「2つの非負の整数を与える...」に変更すると、コメントするための無駄な詳細は残りません。:)
ピーチ

6
@peechこれは真実ではありません。0は、いくつかの定義では自然数と見なされます。削除されたため表示されませんが、この件に関しては広範囲にわたる会話がありました。
小麦ウィザード

回答:


36

RProgN4 3 1バイト

取り消し線4はまだ4です;(

E

最も単純なソリューションは、AとBが等しいかどうかを比較します。trueをプッシュします。同じ場合はRProgNは1として、そうでない場合はfalseとして0を認識します。

テストケース

0+0 = 1
1+0 = 0
0+1 = 0
1+1 = 1

オンラインでお試しください!


22
あなたのバツ印のリンクでウサギの穴を下ったところです。<s> 4 </ s> / 4と評価
Rohan Jhunjhunwala

2
@RohanJhunjhunwalaは私の4を保持、私は行くつもりです
アルバートレンショー

4
̶4̶<-u + 0336(キャラクターの組み合わせ)がより良い方法かもしれません
アルバートレンショー


3
@AlbertRenshaw古いPPCGは4-a-rooを消しましたか?
Rɪᴋᴇʀ

15

網膜、3バイト

 
1

オンラインでお試しください!

(最初の行には改行の前にスペースがあります。StackExchangeは末尾の空白を表示するのにあまり適していません。)

入力は、スペースで区切られた10進数の数値です(例:)12 34。このプログラムは、スペースをaに変更するだけ1で、入力数値の合計には大きすぎる数値を作成します(必然的に少なくとも2桁以上あり、2つの数値を追加すると1桁以下の出力が生成されますより大きな入力)。


2
0 0動作するはずです。
デニス

1
@デニス:私はそれについて疑問に思っていました。010基本的にすべての整数パーサーを介して整数と見なされます。一部の整数パーサーを介して無効な8進数と見なさ0 8れることに基づいて、無効な潜在的な引数を確認できます018(ただし、他の人によっては10進数の18と見なされます)。このプログラムは、inputの先行ゼロを処理して、それらを10進数として扱います。また、他の質問の先頭にゼロを出力するプログラムを作成しましたが、問題は見られません。件名に関連するメタ投稿はありますか?

2
または. 1、自分で先行ゼロを返したくない場合。
マーティンエンダー

@MartinEnder>しかし、自然数
wizzwizz4

@ wizzwizz4フォローしていません。
マーティンエンダー

15

Python、13バイト

[(0,0)].count

オンラインでお試しください!入力をタプルとして受け取ります。

関数にオブジェクトメソッドを使用すると、のボイラープレートが回避されlambdaます。

lambda a,b:a-~b   # 15 bytes

ここで、アイデアはマップすることです (0,0)1、他のすべてすることです0。自然数0+00中の合計のみを与えるので、それは常に合計のマッチングを避けます。

ここでブール値を出力できれば、私は怪しいと思うので、バイトを次のように保存できます。

(0,0).__ge__

これは、入力タプルが最大(0,0)であるかどうかをチェックし(0,0)ます。Pythonでは、True==1False==0。さらに恥ずかしいことに、終了コードを介して出力し、それをPython Boolenとして処理すると、2バイト節約されます。

[(0,0)].pop

文字列I / Oが許可され、先行ゼロがOKの場合、8バイトのソリューションがあります

'1'.join

これは連結しますa1b。これは常により大きいですa+b


1
int.__eq__10バイトの場合
ブルー

@muddyfishええ、私も勝者の答えを見ました、平等を使うことを考えていませんでした。しかし、boolを返します。これは、数値の出力を要求するチャレンジについて不確かだと思います。
xnor

1
IMOが数字のように泳ぎ、数字のように鳴る場合、数字であると仮定するのは合理的です。
電卓

13

MATL、他 1バイト

=

2つの自然数を入力として受け入れ、それらを比較します。それらが等しい場合、出力はで1あり、等しくない場合、出力は0です。これは、@ ATacoのソリューションと同じアプローチです。


3
この=ソリューションはJellyでも1バイト動作します。些細な解決策のために別個の答えを作成する価値はないように思えるので、コメントでそれを言及すると思った。

@ ais523それを更新して更新しました。ありがとう。
Suever


APLとJを追加できますか?
アダム

@Adámもちろん。リンクできるTIOリンクはありますか?
Suever

10

Javascript、10バイト

x=>y=>!x+y

次のようなカリー化構文を使用して2つの数値を取ります。

(x=>y=>!x+y)(0)(0) // 1

4
サイトへようこそ!:)
DJMcMayhem

おかげで=)私はしばらくの間、課題を読んで、ちょうど良い場所を見つけようとしています。
マリビル

9

Vim、3バイト/キーストローク

<C-a>gJ

オンラインでお試しください!

<C-a>ctrl-aであり、byteを表していることに注意してください0x01

vim(プログラミング言語ではない)がゴルフ言語と競合できる場合、それが大好きです。:)入力は次の形式で提供されます。

a
b

これは、最初の数値を1つ増やし(これが<C-a>一部です)、2つの数値の文字列表現を結合します。私が知る限り、これは決して合計にならないはずです。


9

Brain-Flak、8バイト

({}{}())

オンラインでお試しください!

これは私がこれまでに書いた中で最も読みやすい頭脳の答えです。:)

説明:

(      )    # Push the sum of all the following:
 {}         #   The first input
   {}       #   The second input
     ()     #   and one

代替ソリューション(8バイトも):

({}[]{})    # Sum + 1
([]{}{})    # Sum + 2

正の数でのみ機能する他のソリューションがたくさんあります。

(<{}>{})
({}<{}>)
({{}}())
({{}()})
({{}}[])
({{}[]})

7

ゼリー、2バイト

+‘

+は2つの入力を加算し、 'は回答を1つ増やします

オンラインでお試しください!


この答えは私を夢中にさせます。
MD XF

そうだと思います:P
クリストファー

技術的にはこれはそうではありません(a+b)+1a+(b+1)、dyad-monadチェーンfGはとして扱われf(a, G(b))ます。この場合、同じことですが、技術的にはどのように機能するかが異なります:P
HyperNeutrino

6

TI-Basic、3バイト

not(max(Ans

代替ソリューション:

10^(sum(Ans         3 bytes @DestructibleWatermelon
not(sum(Ans         3 bytes
1+sum(Ans           4 bytes
Input :X=Y          5 bytes @ATaco
Input :X+not(Y      6 bytes
Input :not(X+Y      6 bytes
Input :10^(X+Y      6 bytes
Input :X+Y+1        7 bytes
Input :not(max(X,Y  7 bytes
Ans(1)=Ans(2        8 bytes
Ans(1)+not(Ans(2    9 bytes
not(Ans(1)+Ans(2    9 bytes

質問の例をTI-Basicで作成したのは興味深いことですが、短い方を忘れていましたA=B(または、それを見つけるのは私たち次第でしたか?)


1
OPが質問に非常に短い解決策を投稿し、打ち負かすのが難しい場合、誰もそれを好みません。
mbomb007

@ mbomb007と思いますが、これらは単なるスニペットであり、完全なプログラムではありませんでした。Prompt A,B:それらを追加すると、バイトカウントはそれぞれ8バイトになります。
ティムテック

1
@Timtechまさに。例として良い答えをしたくはありませんでした。例が欲しいだけです。
ジュリアンラクニエット

@JulianLachniet私はそれを理解し、感謝しています:)
ティムテック


5

Mathematica、5バイト

1+##&

2つの引数と1の合計を出力し1+##&[2,5]ます8。たとえば、yields 。

(サイドノート:Binomialほとんど動作しますが、反例です; しかしBinomial[1,0]=1Binomial[4,2]=6それらは唯一の反例だと思います。)


Pochhammer〜より良いようですBinomial。私が知る限り、1,0失敗するだけです。
マーティンエンダー

ああ、KroneckerDeltaすべての入力に対して機能します(一部のエソランの等価性チェックと同等です)。実際にはとして再実装する方が短いBoole[#==#2]&ですが、そのまま動作する組み込み機能を探していたと思います。
マーティンエンダー

5

PHP、17バイト

<?=1-join($argv);

次のように実行します:

echo '<?=1-join($argv);' | php -- 0 0
> 1

説明

これは引数を連結するだけです。最初の引数(スクリプト名)にはが含まれます-。その結果、負の数になり、マイナス記号で否定します。次に、最初の入力番号が0(0123 = 123)の場合に備えて、1を追加します。



4

Turtlèd、12バイト

非常に大きな数になります

'1?:?:[1'0l]

オンラインでお試しください!

説明:

'1                write one on the starting grid square
  ?:?:            take a number, move right that many (repeat)
      [1   ]      while not on a grid square with a one on it
        '0l       put a zero on that square, move left
[implicit output of grid]

したがって、10 **(x + y)を出力します。



4

Java (OpenJDK 9), 10 bytes

a->b->a-~b

Try it online!


1
With currying, you can spare a byte: a->b->a-~b. Also works with Java 8, any edition (so there's no need to specify the OpenJDK 9)
Olivier Grégoire

@OlivierGrégoire Java has started to look like JS now >_>
Kritixi Lithos

@KritixiLithos Well... we had a hint this would happen for years: JavaScript ;-)
Olivier Grégoire

@KritixiLithos The specification for Java 9 has a section on 'ECMAScript 6 Compliance'.
Pavel

@OlivierGrégoire Yeah, but this submission was generates automatically, which is why the 9 was added.
Pavel

4

HODOR, 40 bytes (non-competing)

This is probably the shortest program Hodor's ever written!

This is what happens when you have nothing to do for a 2-week school holiday: produce a bunch of really easily coded joke languages that do absolutely nothing. Yay for school holidays!!!

Walder
Hodor?!
Hodor?!
Hodor HODOR!
HODOR!!!

Walder was Hodor's original name and so is needed to begin the program.

Hodor?! takes either a number from STDIN or a single character and sets the accumulator to the input

Hodor add 1 to the accumulator

HODOR! outputs the accumulator as a number

HODOR!!! kills Hodor! Noooooo!

This is the pseudo code:

Take input
Take input
Add 1 to sum(inputs)
Output value

1
I think you need to come up with a different name for your language unless this is an interpreter of the pre-existing Hodor language created in 2015, which I'm fairly certain this isn't.
mbomb007

@mbomb007 No that isn't mine but there are languages with duplicate names. I know of two called 7 on this site (I just can't find them at the moment)
caird coinheringaahing

1
I'm pretty sure there's only one called 7, and it's this one. You could change the capitalization of the title for an easy fix, something like HODOR.
mbomb007

@mbomb007 mine is Hodor and theirs is hodor so that could be enough?
caird coinheringaahing

No, theirs has a capital H, as you can see from every time he uses the language name on his website.
mbomb007

3

SmileBASIC, 4 bytes

!A+B

not(A)+B
1+1=2 -> !1+1 -> 0+1=1
0+1=1 -> !0+1 -> 1+1=2


Out of curiosity, how does this support 2+1?
ATaco

2
2+1=3 -> !2+1 -> 0+1=1
12Me21

3

R, 13 bytes

sum(scan()+1)

Thanks to Jonathan Allan for his inputs !


@JonathanAllan : You're right, I changed my answer. Thanks !
Frédéric

OK, pretty sure 00 is the same as 0 though, maybe sep="1"?
Jonathan Allan

@JonathanAllan : damnit ! Thanks again !
Frédéric

Looking at the Tips For Golfing In R it seems scan() should be fine, accepting a vector input, like this. But we can do one byte better with cat(sum(scan()+1)). Maybe there is shorter?
Jonathan Allan

1
With the cat it is a full program, the alternative would be an unnamed function for the same byte cost function(a,b)a+b+1
Jonathan Allan

3

05AB1E, 1 byte

Q

Works the same as the RProgN answer.

Checks if a and b are the same. If so, print 1. Otherwise, print 0

Try it online!


3
¢ ( a.count(b) ) should also work for 1 byte.
Riley

@Riley you could post that as your own answer.
Okx

2
It's not different enough to needs it's own answer. I thought we could just combine both 1 byte solutions into one answer.
Riley

3

C 26 24 19 bytes

f(c,d){return!c+d;}

Ungolfed version:

int f(int c,int d)
{
   return !c+d; 
}

I hope I got the specification right. Can definitely be shortened!?

@Pavel Thanks for saving 2 bytes

@Neil Thanks for your input.


1
Do you need () around !c+d?
Pavel

@Pavel You're right, brackets were useless, updated!
Abel Tom

2
Not 100% sure but I think you can remove the space in your return, like return!c+d;
Metoniem

1
Lose the return and instead assign it with something like c+=!d
Ahemone

1
@AlbertRenshaw It's not something I'd rely on for portability but here are a couple of examples. I can't get it to work offline and it seems that it needs to be assigned to a non argument variable on TIO.. tio.run/nexus/…
Ahemone

3

MATLAB / Octave, 3 bytes

@eq

Accepts two inputs and checks for equality and yields 1 if they are equal and 0 otherwise.

Online Demo


4
Shouldn't this be @eq? That returns a function handle which can be used to evaluate to the desired function, while just eq is meaningless.
Sanchises

@Sanchises I've seen many answers go both ways: codegolf.stackexchange.com/questions/106149/compute-the-median/…. I'm not actually sure which is preferred.
Suever

Hmmm. I should think this is more like a snippet, while an @ turns it into a valid language construct. But maybe I'm just being pedantic.
Sanchises

3

brainfuck, 12 bytes

Simple solution that outputs A+B+1.

,>,[-<+>]<+.

Try it online


Alternate answer (12 bytes): ,>,[-<++>]<.
Julian Lachniet

@JulianLachniet will that output A+2B?
george

A+2B hacked when B=0
l4m2

@mbomb007 I'm saying the ,>,[-<++>]<. solution
l4m2

@JulianLachniet Yeah, that's not a valid answer because A+2B for input B=0, gives A.
mbomb007

3

dc, 5 bytes

?1n+n

Try it online!

Input: Two natural numbers separated by a space on stdin.

Output: The digit 1 immediately followed by the sum of the two numbers, which is a number larger than the sum of the two numbers.

Example:

Input: 222 333

Output: 1555


3

PHP, 13 bytes; (17 REPL-less)

!max($argv)+0

Examples

[0,0] -> 1
[0,1] -> 0
[1,0] -> 0

For those without REPL use

<?=!max($argv)+0;

and run using

echo '<?=!max($argv)+0;' | php -- 0 0

This answer is not valid because it doesn't output anything
aross

@aross If boolean cast was problem I updated my answer
mleko

Yes, you addressed both problems. The output would be true/false, not 1/0. Also, REPL :)
aross

3

Cubix, 9 8 bytes

u-~OII/@

Explanation

Expanded, this answer looks like this:

    u -
    ~ O
I I / @ . . . .
. . . . . . . .
    . .
    . .

The order of the instructions that are executed is II~-O@

II~-O@
I      # First input
   -   # Minus
 I~    # NOT(second input)
    O  # Output as integer
     @ # End program

Tested for all combinations of inputs where both are in the range 0-100.

Try it here.


3

APL - 4 Bytes

1++/

Takes array, sums its elements and adds one. Test:

 1++/1 2  
4
 1++/1 0
2


3

Hexagony, 7 bytes

?<.!?)@

Try it online!

Or in more readable format,

 ? <
. ! ?
 ) @

This beats the current Hexagony solution of 11 bytes.

Explanation:

If the first number is not 0, the program takes the following path:

Not 0

This reads the first number and branches right. Then it reads the second number, followed by wrapping and trying to read a third, but that doesn't exist so it reads 0. This is printed and the program terminated (note that if a>0, since b is non-negative a+b>0).

If the first number is 0, the program takes the following path to start with:

Is 0

This reads the first number and branches left. It hits the corner, taking the route from along the north-west edge because the number is 0, and reads the second number. It wraps, then increments the second number and prints.

Is still 0

It bounces against the <, printing the incremented second input again. It increments the value and takes the north-east edge again, but this time because the current edge a twice-incremented non-negative value which is definitely positive. It then tries to get a third input, but receives 0 instead.

Is still 0 some more

Finally it wraps and gets diverted by the arrow, then tries to read a fourth input and gets 0 again. It wraps and tries to read a fifth input and receives 0 for the last time. This it prints, and wraps to the @ and exits.

Note that b*(10^k+1)*10>0+b=b where k is the length of b in digits, so this works.


2

Billiards, 11 characters = 17 bytes

⇲
⇲
+
1
+
↥

Implements x + y + 1. Pretty elementary. It takes the inputs on two separate lines. (By the way, the language was modified slightly after the challenge, but only to suppress the prompt from inputting, not sure if this answer is still valid).

7 characters = 11 bytes, non-competing

This one is shorter but only possible after a new update of the language:

⇲
⇲
=
$

This uses x == y, which was shamelessly stolen from @ATaco's RProgN solution [ hope you don't mind (: ]. The $, on exit, outputs how many balls passed over it.


slightly after the language typo, also I think when the language had the input prompt, it would still be valid. I don't think output standards are that strict, and non-suppressible outputs are allowed I think
Destructible Lemon

@DestructibleWatermelon Oh, that was a weird typo. Thanks for the clarification. I actually modified the language, then came and saw the challenge, so I decided to add that note in there in case anyone decided to be picky about checking when the push was made to GitHub, but thank you for clarifying that.
HyperNeutrino
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.