public void Print(int i, int max) {
if ( i < max ) {
Print(i+1, max);
}
Console.Write(i);
Console.Write(" ");
}
public void Main(string[] args) {
int max = Int32.Parse(args[0]);
Print(1, max);
}
How about adding and recursion?
public void Print(int i, int max) {
if ( i < max ) {
Print(i+1, max);
}
Console.Write(i);
Console.Write(" ");
}
public void Main(string[] args) {
int max = Int32.Parse(args[0]);
Print(1, max);
}
void Decrement(int& i)
{
double d = i * i;
d = d / (((double)i)+0.000001); // d ends up being just smaller than i
i = (int)d; // conversion back to an int rounds down.
}
void Print(int max)
{
for( int i = max; i > 0; Decrement(i) )
{
printf("%d ", i);
}
}
use a rounding error:
void Decrement(int& i)
{
double d = i * i;
d = d / (((double)i)+0.000001); // d ends up being just smaller than i
i = (int)d; // conversion back to an int rounds down.
}
void Print(int max)
{
for( int i = max; i > 0; Decrement(i) )
{
printf("%d ", i);
}
}
#include <iostream>
#include <stdlib.h>
int main( int argc, char **argv ) {
for ( unsigned int value = atoi( argv[ 1 ] ); value; ) {
std::cout << value << " ";
for ( unsigned int place = 1; place; place <<= 1 )
if ( value & place ) {
value &= ~place;
break;
} else
value |= place;
}
std::cout << std::endl;
}
Bitwise Arithmetic
Constant space, with no additions, subtractions, multiplications, divisions, modulos or arithmetic negations:
#include <iostream>
#include <stdlib.h>
int main( int argc, char **argv ) {
for ( unsigned int value = atoi( argv[ 1 ] ); value; ) {
std::cout << value << " ";
for ( unsigned int place = 1; place; place <<= 1 )
if ( value & place ) {
value &= ~place;
break;
} else
value |= place;
}
std::cout << std::endl;
}
sealed abstract class Number
case class Elem(num: Number, value: Int) extends Number
case object Nil extends Number
var num: Number = Nil
for (i <- 1 until param)
num = Elem(num, i)
while (num != null)
num match {
case Elem(n, v) => {
System.out.print(v + " ")
num = n
}
case Nil => {
System.out.println("")
num = null
}
}
Quick and dirty version in Scala:
sealed abstract class Number
case class Elem(num: Number, value: Int) extends Number
case object Nil extends Number
var num: Number = Nil
for (i <- 1 until param)
num = Elem(num, i)
while (num != null)
num match {
case Elem(n, v) => {
System.out.print(v + " ")
num = n
}
case Nil => {
System.out.println("")
num = null
}
}
public class CountUp
{
public static void main(String[] args)
{
int n = Integer.parseInt(args[0]);
while (n != 0)
{
System.out.print(n + " ");
n = (int)(n + 0xffffffffL);
}
}
}
public class CountUp
{
public static void main(String[] args)
{
int n = Integer.parseInt(args[0]);
while (n != 0)
{
System.out.print(n + " ");
n = (int)(n + 0xffffffffL);
}
}
}
// count up until found the number. the previous number counted is
// the decremented value wanted.
void Decrement(int& i)
{
int theLastOneWas;
for( int isThisIt = 0; isThisIt < i; ++isThisIt )
{
theLastOneWas = isThisIt;
}
i = theLastOneWas;
}
void Print(int max)
{
for( int i = max; i > 0; Decrement(i) )
{
printf("%d ", i);
}
}
// count up until found the number. the previous number counted is
// the decremented value wanted.
void Decrement(int& i)
{
int theLastOneWas;
for( int isThisIt = 0; isThisIt < i; ++isThisIt )
{
theLastOneWas = isThisIt;
}
i = theLastOneWas;
}
void Print(int max)
{
for( int i = max; i > 0; Decrement(i) )
{
printf("%d ", i);
}
}
import System.Environment (getArgs)
func :: Integer -> [String]
func 0 = []
func n@(x+1) = show n:func x
main = putStrLn . unwords . func . read . head =<< getArgs
A 'feature' called n+k patterns allows this: pattern matching on the addition of two numbers. It is generally not used. A more idiomatic way to do it is with this version of func:
我喜欢 Dylan Bennett 的想法——简单、务实,并且它遵循 KISS 原则,恕我直言,这是我们在开发软件时应该始终牢记的最重要的概念之一。 毕竟,我们编写代码主要是为了其他人来维护它,而不是为了让计算机读取它。 Dylan 用古老的 C 语言给出的解决方案:
#include <stdio.h>
int main(void) {
int n;
for (n = 7; n <= 49; n += 7) {
printf("%d ", n % 8);
}
}
I like Dylan Bennett's idea - simple, pragmatic and it adheres to the K.I.S.S principle, which IMHO is one of the most important concepts we should always try to keep in mind when we develop software. After all we write code primarily for other human beings to maintain it, and not for computers to read it. Dylan's solution in good old C:
#include <stdio.h>
int main(void) {
int n;
for (n = 7; n <= 49; n += 7) {
printf("%d ", n % 8);
}
}
(defun complex-decrement (n)
"Decrements N by adding i squared."
(+ n (expt (complex 0 1) 2)))
(loop for n = 7 then (complex-decrement n)
while (> n 0) do (print n))
Common Lisp
Counting down from 7 (with recursion, or like here, using loop and downto):
(loop for n from 7 downto 1 do (print n))
Alternatively, perhaps a more amusing soluting. Using complex numbers, we simply add i squared repeatedly:
(defun complex-decrement (n)
"Decrements N by adding i squared."
(+ n (expt (complex 0 1) 2)))
(loop for n = 7 then (complex-decrement n)
while (> n 0) do (print n))
发布评论
评论(30)
加法和递归怎么样?
How about adding and recursion?
这是您错过的一种方法,反复试验:
Here's a method you missed, trial and error:
将 1-7 压入堆栈。 一一弹出堆栈。 打印 7-1。 :)
Push 1-7 onto a stack. Pop stack one by one. Print 7-1. :)
使用 2 的补码,毕竟这是计算机处理负数的方式。
请参阅http://en.wikipedia.org/wiki/2's_complement
use 2's compliment, after all this is how a computer deals with negative numbers.
see http://en.wikipedia.org/wiki/2's_complement
将数字添加到字符串缓冲区中。
Prepend the numbers into a string buffer.
c/c++,一点算术溢出:
c/c++, a bit of arithmetic overflow:
我注意到没有人发布最愚蠢的答案,所以我将继续分享它:
不要恨我:看到了吗? 我承认这很愚蠢。 :)
I note that nobody posted the stupidest possible answer, so I'll go ahead and share it:
Don't hate me: See? I admitted it's stupid. :)
使用舍入误差:
use a rounding error:
按位算术
常数空间,没有加法、减法、乘法、除法、模数或算术负数:
Bitwise Arithmetic
Constant space, with no additions, subtractions, multiplications, divisions, modulos or arithmetic negations:
这并不难。 使用模运算符。
This is not hard. Use the modulus operator.
一个Python版本:
A python version:
这是作弊吧?
只是为了好玩,它的递归兄弟:
This is cheating, right?
And just for fun, its recursive brother:
从包含从大到小您感兴趣的递减数字的文件开始:
然后...这只适用于 9999
Start with a file containing descending numbers from to the max you're interested in:
Then... this only works up to 9999
Scala 中的快速而肮脏的版本:
Quick and dirty version in Scala:
增加传递给 max_int 的有符号整数,然后将其“添加”到计数器中...或者这是否被认为是非法减法?
Increment a signed integer passed max_int and then "Add" it to the counter... or is this consider illegitimate subtraction?
有点讨厌,但它确实有效
Kinda nasty but it does the job
我们这是在打高尔夫球吗?
Are we golfing this?
Haskell:
一个称为 n+k 模式的“功能”允许这样:对两个数字相加进行模式匹配。 一般不使用。 一种更惯用的方法是使用此版本的 func:
或每行一个数字:
Haskell:
A 'feature' called n+k patterns allows this: pattern matching on the addition of two numbers. It is generally not used. A more idiomatic way to do it is with this version of func:
or, with one number per line:
这算不算? 仅使用添加指令...
Does this count? Only uses an add instruction...
珀尔:
Perl:
减法无论如何都是一种幻觉
subtraction is an illusion anyways
我喜欢 Dylan Bennett 的想法——简单、务实,并且它遵循 KISS 原则,恕我直言,这是我们在开发软件时应该始终牢记的最重要的概念之一。 毕竟,我们编写代码主要是为了其他人来维护它,而不是为了让计算机读取它。 Dylan 用古老的 C 语言给出的解决方案:
I like Dylan Bennett's idea - simple, pragmatic and it adheres to the K.I.S.S principle, which IMHO is one of the most important concepts we should always try to keep in mind when we develop software. After all we write code primarily for other human beings to maintain it, and not for computers to read it. Dylan's solution in good old C:
在 C 中,使用旋转内存块(注意,不是我引以为傲的东西......):
以及将列表视为整数的常见 lisp 解决方案:
将其变成可执行文件可能是最难的部分,并作为练习留给读者。
In C, using a rotating memory block (note, not something I'm proud of...):
And a common lisp solution treating lists as ints:
Making this into an executable is probably the hardest part and is left as an exercise to the reader.
Common Lisp
从 7 开始倒数(使用递归,或者像这里一样,使用
loop
和downto
):(loop for n from 7 downto 1 do (print n) )
或者,也许是一个更有趣的解决方案。 使用复数,我们只需重复添加 i 的平方即可:
Common Lisp
Counting down from 7 (with recursion, or like here, using
loop
anddownto
):(loop for n from 7 downto 1 do (print n))
Alternatively, perhaps a more amusing soluting. Using complex numbers, we simply add i squared repeatedly:
我喜欢递归
你也可以使用乘法
I like recursive
You can also use multiplication
另一种 Perl 版本可能是:
An alternative perl version could be: