描述
给定一个数字,1以二进制表示形式打印它具有的s 数量。
输入值
>= 0以10为底的数字不会超过您的语言能够处理的最高数字。
输出量
1s 的数量(以二进制表示)。
获奖条件
最短的代码获胜。
不允许的
- 按位运算符。允许其他运算符,例如加法和乘法。
- 内置基本转换功能。
例子
Input: Ouput:
56432 8
Input: Output:
45781254 11
Input: Output:
0 0
给定一个数字,1以二进制表示形式打印它具有的s 数量。
>= 0以10为底的数字不会超过您的语言能够处理的最高数字。
1s 的数量(以二进制表示)。
最短的代码获胜。
Input: Ouput:
56432 8
Input: Output:
45781254 11
Input: Output:
0 0
Answers:
+/2|⌊⎕÷2*⍳32
假定解释器使用32位整数,并且将⎕IO其设置为0(表示monadic ⍳以0而不是1开头)。我使用了32位版本的Dyalog APL。
从右到左的解释:
⍳32生成一个前32整数的向量(如前所述,因为⎕IO为0,所以该向量从0开始)。*是幂函数。在这种情况下,它生成2作为其右引数提供的向量的每个元素的幂。÷是除法功能。它使我们⎕(评估的用户输入)除以矢量右边的每个元素(每个为2的幂)。⌊ 将参数的每个元素置于其右边。2| 给我们右边的每个元素的其余部分除以 2。/ 使用其左侧的函数减少(折叠)其右侧参数, +。不再是9个字符了。:(
违反规则的旧版本:
+/⎕⊤⍨32/2从右到左的解释:
32/2:复制2,32次数。⍨将二进位函数换向其左侧,在这种情况下为⊤(即,X⊤⍨Y等于Y⊤X)。⊤是编码功能。它在给定其左边的底数中对右边的整数进行编码。注意,由于使用了通勤运算符,左右参数将被切换。对所需的位数重复基数,因此32/2。⎕是一个尼拉度函数,它接受用户输入并对其进行评估。+/使用减少(折叠)其正确的参数+。(我们将0和1相加。)
Built-in base conversion functions约束吗?
,.
在我看来(“您的语言能够处理的最高数字”)(以及大多数答案使用的是最合理的解释)是“您的语言本机支持的最大数字”。Brainbool是一个头脑混乱的派生类,它使用位而不是字节,并以二进制(0和1字符)而不是字符代码的形式进行输入和输出。因此1,最大的本机支持数字是,而最小的是0,分别具有1和的汉明权重0。
根据Esolang的说法,Brainbool成立于2010年。
(+数字中的位数)
+/2|<.n%2^i.32
用法:更换 n程序中的为要测试的编号。
例子:
+/2|<.56432%2^i.32
8
+/2|<.45781254%2^i.32
11
+/2|<.0%2^i.32
0
可能有一种重新排列的方式,因此可以将数字放在开头或结尾,但这是我的第一个J条目,现在我的头部略有伤痛。
说明(主要是为了将来我能理解)
i.32 -创建数字1到32的数组
2^ -将列表转换为两个1到4294967296的幂
n% -将输入数字除以列表中的每个元素
<. -将所有除法结果四舍五入到下一个整数
2|-与%2大多数语言相同-如果是偶数则返回0,如果是奇数则返回1
+/ -总计列表中的项目(现在仅为1或0)
这缺少强制性的Brainfuck解决方案,所以我做了一个:
[[->+<[->->>>+<<]>[->>>>+<<]<<<]>>>>[-<<<<+>>>>]<<<<]
从单元格1中提取数字并将结果放入单元格6中。
未注册和评论的版本:
[ while n != 0
[ div 2 loop
-
>+< marker for if/else
[->->>>+<<] if n != 0 inc n/2
>
[->>>>+<<] else inc m
<<<
]
>>>> move n/2 back to n
[-<<<<+>>>>]
<<<<
]
t,n=0,input()
while n:t+=n%2;n/=2
print t
注意:我的其他答案使用lambda和递归,而这个答案使用while循环。我认为它们足够不同,因此有两个答案。
b=lambda n:n and n%2+b(n/2)
print b(input())
def代替lambda 缩短两个字符。
print b(input())。返回值并将“输入”作为函数的参数是可以接受的。
$n=<>;while($n){$_+=$n%2;$n/=2}print
感谢Howard的45-> 43,以及User606723的43-> 36。
$n=int($n/2)短2个字符的字符。
$n=<>;while($n){$_+=$n%2;$n/=2}print这将一直循环直到$ n / 2最终足够接近于0,但是我们在乎吗?;)
$==<>;1while$_+=$=%2,$=/=2;say
基于PhiNotPi的解决方案,并进行了一些额外的打高尔夫球。运行perl -M5.010以启用Perl 5.10 say功能。
$=特变做你的程序有什么特别,或者是它只是一个普通的变量?
$=仅采用整数值,因此使用它可以为我节省一个int。
void f(x){int i=0;for(;x;x/=2)i+=x%2;printf("%u",i);}
功能主体只有38个字符。编辑:删除按位运算符编辑:printf按照注释中的建议退出循环编辑:切换到K&R声明;而且,这不再是C99特有的
main(int n,char **a){printf("%u",__builtin_popcount(atoi(a[1])))};
注意:需要gcc或gcc兼容的编译器(例如ICC,clang)。
对于某些CPU,它们会__builtin_popcount编译为一条指令(例如,POPCNT在x86上)。
__builtin_popcount实际上只实现1s本身的计数是否正确?如果是这样,尽管按照规则并没有严格错误,但老实说,我认为这不是一个公平的条目。
printf不能在没有事先包含的情况下使用。
C++
我还将发布最初的解决方案,然后再提出问题。虽然已经有了一个更好的JavaScript答案:)
for(n=prompt(a=0),j=1;j<=n;j*=2)for(i=j;i<=n;i+=2*j)n<i+j&&a++;alert(a)
这个想法来自某些“心灵阅读卡”,通过向他们显示卡片并让他们说出他们的号码在哪些卡片上是显而易见的,从而使您能够获得他人记住的数字。
It works because each number is a unique combination of 1s / 0s in binary. My solution checks on which "cards" the number is apparent so as to determine how many 1s it has. It's just not very efficient, though...
I found this document which outlines the mind reading technique.
$i=$s=0;for(;$i<log($n,2);){$s+=$n/pow(2,$i++)%2;}echo$s;
This assumes that $n holds the value to be tested.
function b($i){return$i|0?($i%2)+b($i/2):0;}echo b($n);
Again, this assumes that $n holds the value to be tested. This is an alternative because it uses the or-operator to floor the input.
Both solutions work and do not cause notices.
Based on @Leah Xue's solution. Three spaces could be removed and it's sligthly shorter (~3 characters) to use function instead of if-then-else.
let rec o=function 0->0|x->(x mod 2)+(o(x/2))
int f(string i){int k=to!int(i),r;while(k){if(k%2)r++;k/=2;}return r;}
o=function(n){h=n%/%2;n%%2+if(h)o(h)else 0};o(scan())
Examples:
> o=function(n){h=n%/%2;n%%2+if(h)o(h)else 0};o(scan())
1: 56432
2:
Read 1 item
[1] 8
> o=function(n){h=n%/%2;n%%2+if(h)o(h)else 0};o(scan())
1: 45781254
2:
Read 1 item
[1] 11
> o=function(n){h=n%/%2;n%%2+if(h)o(h)else 0};o(scan())
1: 0
2:
Read 1 item
[1] 0
If inputting the number is not part of the character count, then it is 43 characters:
o=function(n){h=n%/%2;n%%2+if(h)o(h)else 0}
with test cases
> o(56432)
[1] 8
> o(45781254)
[1] 11
> o(0)
[1] 0
I polished the rules a bit to add to the challenge. The function doesn't care about the base of the number because it uses its own binary scale. I was inspired by the way analog to numeric conversion works. I just use plain recursion for this:
(define (find-ones n)
(define (nbits n)
(let nbits ([i 2])
(if (< i n) (nbits (* i 2)) i)))
(let f ([half (/ (nbits n) 2)] [i 0] [n n])
(cond [(< half 2) i]
[(< n i) (f (/ half 2) i (/ n 2))]
[else (f (/ half 2) (+ i 1) (/ n 2))])))
Isn't reading a number into binary or printing the number from binary a "builtin base conversion function", thus invalidating every answer above that prints an integer? If you permit reading and printing an integer, like almost all the above answers do, then I'll make claims using a builtin popcount function :
There was a popCount routine added to the Data.Bits module for GHC v7.2.1/v7.4.1 this summer (see tickets concerning the primop and binding).
import Data.Bits
main=interact$show.popCount.read
I cannot beat the above Python and Perl scores using their GMPY or GMP::Mpz modules for GMP sadly, although GMP does offer a popcount function too.
for(n=prompt(o=0);n=n/2|0;o+=n%2);alert(o)
Demo: http://jsfiddle.net/hcYdx/4/
Edit 1: remove q and use ~~ for rounding, save 2 chars.
Edit 2: use |0 rounding operator instead of ~~ to save parentheses (2 chars).
Edit 3: simplify n>0 to n and combine with n=n/2|0 to make entire condition; now have wasted statement space :(
|0 a bitwise operator?
| is bitwise operator... it is disallowed. Time to do Math.round :-)
int b(Long a){return a.bitCount(a);}
Because of course this, of all things, is something that Java has a builtin for...
bitCount operates under the hood.
while($n){$o+=$n%2;$n/=2*1;}echo $o;
Assumes $n is the number to be tested, shows a PHP Notice for $o, and doesn't exactly work when $n is 0 (outputs nothing).
$n=$argv[1];$o=0;while($n){$o+=$n%2;$n/=2*1;}echo $o;
Accepts command-line input, doesn't show a PHP Notice, and outputs correctly for 0.