不同语言的阶乘算法 [英] Factorial Algorithms in different languages

查看:126
本文介绍了不同语言的阶乘算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解阶乘子例程或程序的所有不同处理方法.希望任何人都可以来这里看看他们是否想学习一种新语言.

I want to see all the different ways you can come up with, for a factorial subroutine, or program. The hope is that anyone can come here and see if they might want to learn a new language.

  • 程序
  • 功能性
  • 面向对象
  • 一个班轮
  • 迷惑
  • 奇数游戏
  • 错误代码
  • 多边形
  • Procedural
  • Functional
  • Object Oriented
  • One liners
  • Obfuscated
  • Oddball
  • Bad Code
  • Polyglot

基本上,我想看看一个示例,该示例介绍了编写算法的不同方法以及它们在不同语言中的外观.

Basically I want to see an example, of different ways of writing an algorithm, and what they would look like in different languages.

请限制为每个条目仅举一个示例. 如果您想突出显示一种特定的样式,语言或一个经过深思熟虑的想法以使自己适合发表在一篇文章中,那么我将为每个答案提供一个以上的示例.

Please limit it to one example per entry. I will allow you to have more than one example per answer, if you are trying to highlight a specific style, language, or just a well thought out idea that lends itself to being in one post.

唯一真正的要求是,必须以所有表示的语言找到给定参数的阶乘.

The only real requirement is it must find the factorial of a given argument, in all languages represented.


# Language Name: Optional Style type

   - Optional bullet points

    Code Goes Here

Other informational text goes here

我会暂时地编辑所有格式不正确的答案.

I will ocasionally go along and edit any answer that does not have decent formatting.

推荐答案

多边形:5种语言,全部使用bignums

因此,我写了一种多语言的语言,可以用我经常用的三种语言编写,也有我从其他答案中得到的一种,以及我今天刚刚学到的一种.这是一个独立的程序,它读取包含非负整数的单行并打印包含其阶乘的单行. Bignum用于所有语言,因此最大可计算阶乘仅取决于您计算机的资源.

Polyglot: 5 languages, all using bignums

So, I wrote a polyglot which works in the three languages I often write in, as well as one from my other answer to this question and one I just learned today. It's a standalone program, which reads a single line containing a nonnegative integer and prints a single line containing its factorial. Bignums are used in all languages, so the maximum computable factorial depends only on your computer's resources.

  • Perl :使用内置的bignum包.用perl FILENAME运行.
  • Haskell :使用内置的bignums.使用runhugs FILENAME或您喜欢的编译器的等效版本运行.
  • C ++ :需要GMP来支持bignum.要使用g ++进行编译,请使用g++ -lgmpxx -lgmp -x c++ FILENAME链接到正确的库.编译后,运行./a.out.或使用您最喜欢的编译器.
  • brainf * ck :我在穆勒的经典发行版,使用bf < FILENAME > EXECUTABLE进行编译.使输出可执行并运行.或使用您喜欢的发行版.
  • 空白:使用内置的bignum支持.使用wspace FILENAME运行.
  • Perl: uses built-in bignum package. Run with perl FILENAME.
  • Haskell: uses built-in bignums. Run with runhugs FILENAME or your favorite compiler's equivalent.
  • C++: requires GMP for bignum support. To compile with g++, use g++ -lgmpxx -lgmp -x c++ FILENAME to link against the right libraries. After compiling, run ./a.out. Or use your favorite compiler's equivalent.
  • brainf*ck: I wrote some bignum support in this post. Using Muller's classic distribution, compile with bf < FILENAME > EXECUTABLE. Make the output executable and run it. Or use your favorite distribution.
  • Whitespace: uses built-in bignum support. Run with wspace FILENAME.

添加了空白作为第五种语言.顺便说一下,不要用<code>标签包装代码.它打破了空白.另外,该代码在固定宽度上看起来要好得多.

added Whitespace as a fifth language. Incidentally, do not wrap the code with <code> tags; it breaks the Whitespace. Also, the code looks much nicer in fixed-width.

char //# b=0+0{- |0*/; #>>>>,----------[>>>>,--------
#define	a/*#--]>>>>++<<<<<<<<[>++++++[<------>-]<-<<<
#Perl	><><><>	 <> <> <<]>>>>[[>>+<<-]>>[<<+>+>-]<->
#C++	--><><>	<><><><	> < > <	+<[>>>>+<<<-<[-]]>[-]
#Haskell >>]>[-<<<<<[<<<<]>>>>[[>>+<<-]>>[<<+>+>-]>>]
#Whitespace	>>>>[-[>+<-]+>>>>]<<<<[<<<<]<<<<[<<<<
#brainf*ck > < ]>>>>>[>>>[>>>>]>>>>[>>>>]<<<<[[>>>>*/
exp; ;//;#+<<<<-]<<<<]>>>>+<<<<<<<[<<<<][.POLYGLOT^5.
#include <gmpxx.h>//]>>>>-[>>>[>>>>]>>>>[>>>>]<<<<[>>
#define	eval int	main()//>+<<<-]>>>[<<<+>>+>->
#include <iostream>//<]<-[>>+<<[-]]<<[<<<<]>>>>[>[>>>
#define	print std::cout	<< // >	<+<-]>[<<+>+>-]<<[>>>
#define	z std::cin>>//<< +<<<-]>>>[<<<+>>+>-]<->+++++
#define c/*++++[-<[-[>>>>+<<<<-]]>>>>[<<<<+>>>>-]<<*/
#define	abs int $n //><	<]<[>>+<<<<[-]>>[<<+>>-]]>>]<
#define	uc mpz_class fact(int	$n){/*<<<[<<<<]<<<[<<
use bignum;sub#<<]>>>>-]>>>>]>>>[>[-]>>>]<<<<[>>+<<-]
z{$_[0+0]=readline(*STDIN);}sub fact{my($n)=shift;#>>
#[<<+>+>-]<->+<[>-<[-]]>[-<<-<<<<[>>+<<-]>>[<<+>+>+*/
uc;if($n==0){return 1;}return $n*fact($n-1);	}//;#
eval{abs;z($n);print fact($n);print("\n")/*2;};#-]<->
'+<[>-<[-]]>]<<[<<<<]<<<<-[>>+<<-]>>[<<+>+>-]+<[>-+++
-}--	<[-]]>[-<<++++++++++<<<<-[>>+<<-]>>[<<+>+>-++
fact 0	= 1 -- ><><><><	> <><><	]+<[>-<[-]]>]<<[<<+ +
fact	n=n*fact(n-1){-<<]>>>>[[>>+<<-]>>[<<+>+++>+-}
main=do{n<-readLn;print(fact n)}-- +>-]<->+<[>>>>+<<+
{-x<-<[-]]>[-]>>]>]>>>[>>>>]<<<<[>+++++++[<+++++++>-]
<--.<<<<]+written+by+++A+Rex+++2009+.';#+++x-}--x*/;}

这篇关于不同语言的阶乘算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆