知乎周源微信_每周源代码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

相关文章

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示

Redis迷你版微信抢红包实战

《Redis迷你版微信抢红包实战》本文主要介绍了Redis迷你版微信抢红包实战... 目录1 思路分析1.1hCckRX 流程1.2 注意点①拆红包:二倍均值算法②发红包:list③抢红包&记录:hset2 代码实现2.1 拆红包splitRedPacket2.2 发红包sendRedPacket2.3 抢

SpringBoot后端实现小程序微信登录功能实现

《SpringBoot后端实现小程序微信登录功能实现》微信小程序登录是开发者通过微信提供的身份验证机制,获取用户唯一标识(openid)和会话密钥(session_key)的过程,这篇文章给大家介绍S... 目录SpringBoot实现微信小程序登录简介SpringBoot后端实现微信登录SpringBoo

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

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

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

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

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

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

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

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

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