知乎周源微信_每周源代码13-斐波那契版

2023-12-20 11:50

本文主要是介绍知乎周源微信_每周源代码13-斐波那契版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

知乎周源微信

知乎周源微信

iStock_000000237891XSmall

If you're new to this, each week I post some snippets of particularly interesting (read: beautiful, ugly, clever, obscene) source and the project it came from. This started from a belief that reading source is as important (or more so) as writing it. We read computer books to become better programmers, but unless you're reading books like Programming Pearls, you ought to peruse some Open Source projects for inspiration.

如果您是新手,我每周都会发布一些片段,这些片段特别有趣(阅读:美丽,丑陋,聪明,淫秽)和其来源。 这源于一种信念,即阅读源与编写源同样重要(甚至更重要) 。 我们阅读计算机书籍以成为更好的程序员,但是除非您阅读《 Programming Pearls》之类的书,否则您应该细读一些开放源代码项目以获取灵感。

And so, Dear Reader, I present to you the thirteenth in a infinite number of posts of "The Weekly Source Code." Here's some source I was reading this week. I wanted to see what a Fibonacci number generator looked like in a number of different languages.

因此,尊敬的读者,我在“每周源代码”的无数帖子中向您介绍了第十三篇。 这是我本周正在阅读的一些资料。 我想看看用多种不同语言编写的斐波那契数生成器的外观。

Remember (from Wikipedia) that the Fibonacci sequence looks like this:

请记住(来自Wikipedia )斐波那契数列如下所示:

...after two starting values, each number is the sum of the two preceding numbers. The first Fibonacci numbers also denoted as Fn, for n = 0, 1, … , are:

...在两个起始值之后,每个数字是前两个数字的总和。 对于n = 0,1,…的第一个斐波那契数也表示为F n为:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, ... 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368, 75025、121393,...

Here's a few implementations I thought contrasted nicely. There's also a great writeup on Fibonacci related things on Dustin Campbell's blog.

我认为这是一些很好的实现。 在达斯汀·坎贝尔(Dustin Campbell)的博客上,也有很多关于斐波那契相关内容的文章。

F# (F#)

  • Here's a basic Fibonacci function in F#.

    这是F#中的基本斐波那契函数。

    Here's a basic Fibonacci function in F#.

    这是F#中的基本斐波那契函数。

    let rec fib n = if n < 2 then 1 else fib (n-2) + fib(n-1)
    
  • Or, if you want input and output as an F# console application:

    或者,如果要将输入和输出作为F#控制台应用程序:

    Or, if you want input and output as an F# console application:

    或者,如果要将输入和输出作为F#控制台应用程序:

    let fib_number = int_of_string (System.Environment.GetCommandLineArgs().GetValue(1).ToString());;
    let rec fib n = if n < 2 then 1 else fib (n-2) + fib(n-1);;
    Printf.printf "\nThe Fibonacci value of %u is: %u\n" fib_number (fib fib_number);;
    exit 0;;

Ruby (Ruby)

  • Here it is in Ruby, from RubyTips.org:

    这是RubyTips.org中的Ruby:

    Here it is in Ruby, from RubyTips.org:

    这是RubyTips.org中的Ruby:

    x1,x2 = 0,1; 0.upto(size){puts x1; x1 += x2; x1,x2 = x2,x1}
    
  • But that's really hard to read and kind of smooshed onto one line. A more typical console app would look like this:

    但是,这确实很难阅读,而且有点模糊。 一个更典型的控制台应用程序将如下所示:

    But that's really hard to read and kind of smooshed onto one line. A more typical console app would look like this:

    但这确实很难阅读,而且有点模糊。 一个更典型的控制台应用程序将如下所示:

    class FibonacciGenerator   def printFibo(size)   x1,x2 = 0, 1   0.upto(size){puts x1;x1+=x2; x1,x2= x2,x1} # note the swap for the next iteration   end  f = FibonacciGenerator.new  f.printFibo(10) # print first 10 fibo numbers   
    end  

C#(C#)

  • There's many ways to do this in C#, so let's start with a basic C# 2.0 implementation.

    在C#中有很多方法可以做到这一点,所以让我们从一个基本的C#2.0实现开始。

    There's many ways to do this in C#, so let's start with a basic C# 2.0 implementation.

    在C#中有很多方法可以做到这一点,所以让我们从一个基本的C#2.0实现开始。

    static int Fibonacci (int x)
    {if (x <= 1)return 1;return Fibonacci (x-1) + Fibonacci (x-2);
    }	
  • Now, here's a great way using C# 3.0 (actually, .NET 3.5 and System.Func from the new System.Core:

    现在,这是使用C#3.0(实际上是来自新System.Core的.NET 3.5和System.Func)的一种好方法:

    Now, here's a great way using C# 3.0 (actually, .NET 3.5 and System.Func from the new System.Core:

    现在,这是使用C#3.0(实际上是来自新System.Core的.NET 3.5和System.Func)的一种好方法:

    Func<INT , int> fib = null;
    fib = n => n > 1 ? fib(n - 1) + fib(n - 2) : n;

Scala (Scala)

  • Lots of folks are excited about Scala, and many are calling it "the latest programmer's shiny object." It's interesting not just for its syntax, but it's full interoperability with Java.  Here's a recursive Fibonacci in Scala.

    许多人都对Scala感到兴奋,许多人称它为“最新程序员的闪亮对象”。 不仅因为其语法有趣,而且与Java完全互操作。 这是Scala中的递归斐波那契。

    Lots of folks are excited about Scala, and many are calling it "the latest programmer's shiny object." It's interesting not just for its syntax, but it's full interoperability with Java.  Here's a recursive Fibonacci in Scala.

    许多人都对Scala感到兴奋,许多人称它为“最新程序员的闪亮对象”。 不仅因为其语法有趣,而且与Java完全互操作。 这是Scala中的递归斐波那契。

    def fib( n: Int): Int = n match {case 0 => 0case 1 => 1case _ => fib( n -1) + fib( n-2)}

Erlang (Erlang)

  • Here's Fibonacci in Erlang, and you can find many other implementations at LiteratePrograms.org, a great site for reading source!

    这是Erlang的Fibonacci ,您可以在LiteratePrograms.org上找到许多其他实现,这是一个阅读源代码的好网站!

    Here's Fibonacci in Erlang, and you can find many other implementations at LiteratePrograms.org, a great site for reading source!

    这是位于Erlang的Fibonacci ,您可以在LiteratePrograms.org上找到许多其他实现,这是一个阅读源的绝佳网站!

    fibo(0) -> 0 ;
    fibo(1) -> 1 ;
    fibo(N) when N > 0 -> fibo(N-1) + fibo(N-2) .

Which is your favorite? I like the C# 3.0 one and the F# ones. Also the Ruby double variable swap is pretty cool. They just feel right, although a close runner-up is the LOLCode implementation of Fibonacci from a few weeks back.

哪个是你最喜欢的? 我喜欢C#3.0和F#。 Ruby的双变量交换也很酷。 他们只是感觉不错,尽管从几周前开始,斐波那契的LOLCode实现获得了亚军。

翻译自: https://www.hanselman.com/blog/the-weekly-source-code-13-fibonacci-edition

知乎周源微信

这篇关于知乎周源微信_每周源代码13-斐波那契版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/515943

相关文章

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

微信公众号脚本-获取热搜自动新建草稿并发布文章

《微信公众号脚本-获取热搜自动新建草稿并发布文章》本来想写一个自动化发布微信公众号的小绿书的脚本,但是微信公众号官网没有小绿书的接口,那就写一个获取热搜微信普通文章的脚本吧,:本文主要介绍微信公众... 目录介绍思路前期准备环境要求获取接口token获取热搜获取热搜数据下载热搜图片给图片加上标题文字上传图片

如何用java对接微信小程序下单后的发货接口

《如何用java对接微信小程序下单后的发货接口》:本文主要介绍在微信小程序后台实现发货通知的步骤,包括获取Access_token、使用RestTemplate调用发货接口、处理AccessTok... 目录配置参数 调用代码获取Access_token调用发货的接口类注意点总结配置参数 首先需要获取Ac

W外链微信推广短连接怎么做?

制作微信推广链接的难点分析 一、内容创作难度 制作微信推广链接时,首先需要创作有吸引力的内容。这不仅要求内容本身有趣、有价值,还要能够激起人们的分享欲望。对于许多企业和个人来说,尤其是那些缺乏创意和写作能力的人来说,这是制作微信推广链接的一大难点。 二、精准定位难度 微信用户群体庞大,不同用户的需求和兴趣各异。因此,制作推广链接时需要精准定位目标受众,以便更有效地吸引他们点击并分享链接

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

SWAP作物生长模型安装教程、数据制备、敏感性分析、气候变化影响、R模型敏感性分析与贝叶斯优化、Fortran源代码分析、气候数据降尺度与变化影响分析

查看原文>>>全流程SWAP农业模型数据制备、敏感性分析及气候变化影响实践技术应用 SWAP模型是由荷兰瓦赫宁根大学开发的先进农作物模型,它综合考虑了土壤-水分-大气以及植被间的相互作用;是一种描述作物生长过程的一种机理性作物生长模型。它不但运用Richard方程,使其能够精确的模拟土壤中水分的运动,而且耦合了WOFOST作物模型使作物的生长描述更为科学。 本文让更多的科研人员和农业工作者

uniapp设置微信小程序的交互反馈

链接:uni.showToast(OBJECT) | uni-app官网 (dcloud.net.cn) 设置操作成功的弹窗: title是我们弹窗提示的文字 showToast是我们在加载的时候进入就会弹出的提示。 2.设置失败的提示窗口和标签 icon:'error'是设置我们失败的logo 设置的文字上限是7个文字,如果需要设置的提示文字过长就需要设置icon并给

GitHub每周最火火火项目(9.2-9.8)

项目名称:polarsource / polar 项目介绍:polar 是一个开源项目,它是 Lemon Squeezy 的替代方案,并且具有更具优势的价格。该项目的目标是为开发者提供一种更好的选择,让他们能够在追求自己的热情和兴趣的同时,通过编码获得相应的报酬。通过使用 polar,开发者可以享受到更实惠的价格,同时也能够更自由地发挥自己的创造力和技能。 项目地址:https://github.

13 transition数组的动画使用

划重点 动画:transitiontransition-group :数组动画数组的 添加 / 删除 豆腐粉丝汤 清淡又健康 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><me

【CTF Web】BUUCTF Upload-Labs-Linux Pass-13 Writeup(文件上传+PHP+文件包含漏洞+PNG图片马)

Upload-Labs-Linux 1 点击部署靶机。 简介 upload-labs是一个使用php语言编写的,专门收集渗透测试和CTF中遇到的各种上传漏洞的靶场。旨在帮助大家对上传漏洞有一个全面的了解。目前一共20关,每一关都包含着不同上传方式。 注意 1.每一关没有固定的通关方法,大家不要自限思维! 2.本项目提供的writeup只是起一个参考作用,希望大家可以分享出自己的通关思路