首页 \ 问答 \ Fibonacci序列 - PHP最短代码(Fibonacci Sequence - PHP shortest code possible)

Fibonacci序列 - PHP最短代码(Fibonacci Sequence - PHP shortest code possible)

我花了一些时间尝试将Fibonacci序列执行到500个数字并以尽可能短的代码输出到屏幕。 这对我来说是一次学习练习。

我把它从21行压缩到12行,这是我能编写的最短代码,这使得这个工作..任何人都可以告诉我如何使代码更短?

我已经在google上查找了PHP循环,而while循环似乎效果最好。 PHP中是否还有其他数学技巧可以用来压缩这个?

通常当我问一个问题时,我喜欢展示我对这个问题所做的研究,但由于我不知道任何关键字要查找更好的数学或循环,我不知道该搜索什么...

码:

$counter = 0;
$first = 1;
$second = 1;
echo $first."<br/>";
echo $second."<br/>";
while ($counter < 500) {
  $next = $first + $second;
  echo $next."<br/>";
  $counter++;
  $second=$first;
  $first=$next;
}

I have spent some time trying to execute the Fibonacci sequence to 500 numbers and output to screen in the shortest amount of code possible. This was a learning exercise for me.

I have condensed it from 21 lines down to 12, this is the shortest code I can write that makes this work.. Can anyone show me how I could have made the code even shorter?

I have looked on google for PHP loops, and the while loop seemed to work best. Are there any other math tricks in PHP I can use to condense this even more?

Normally when I ask a question, I like to show what research I have done into the problem, but since I don't know any keywords to look up for better math or loops, I am not sure what to search..

Code:

$counter = 0;
$first = 1;
$second = 1;
echo $first."<br/>";
echo $second."<br/>";
while ($counter < 500) {
  $next = $first + $second;
  echo $next."<br/>";
  $counter++;
  $second=$first;
  $first=$next;
}

原文:
更新时间:2021-12-09 10:12

最满意答案

我们发现了这个问题。 使用单字符参数定义URI参数存在问题。 作为解决方法,请使用不同的参数定义您的资源。 例如:/ client / calculate / add / {xx} / {yy}

此外,您必须将资源的“Produces”属性设置为“application / xml”,因为默认值为“application / json”,并且不受后端支持。

请参考下面的截图并定义您的API资源,并重新发布API。

在这里输入图像描述


We found the issue. There is a problem with defining URI params with single-character parameters. As a workaround, please define your resource with different parameters. Ex: /client/calculate/add/{xx}/{yy}

Also, you have to set the "Produces" attribute for the resource as "application/xml" since the default value is "application/json" and is not supported by your backend.

Please refer the screenshot below and define your API resource, and republish the API.

enter image description here

相关问答

更多
  • 网址中的版本应为v0.10 http://localhost:9763/client-registration/v0.10/register Version in the URL should be v0.10 http://localhost:9763/client-registration/v0.10/register
  • 如果您想将WSO2 IS配置为关键管理器,则可以在WSO2文档中找到所有详细步骤https://docs.wso2.com/display/CLUSTER44x/Configuring+the+Identity+Server+5.2.0+as+a+键+管理+与+ API +管理+ 2.0.0 If you want to configure WSO2 IS as an keymanager all the detailed steps can be found in WSO2 doc https://doc ...
  • WSO2IS可以用作身份提供者。 在这种情况下,是的,它将负责验证用户(有很多方法可以做到这一点,您可以选择适合您需求的方法)。 它可以使用任何SSO协议,如SAML,OpenID Connect等等。 是的,这是我认为最常见的方法。 通常每个组织都有自己的用户信息存储区。 它可能是LDAP或RDBMS等。您可以将WSO2IS设置为接收认证请求,并让WSO2IS从该现有存储中认证用户。 WSO2IS can be used as a identity provider. In that case, yes ...
  • 404表示资源不是资金。 你能看到你的请求URL是对的吗? Eg: http://localhost:8280/test/1.0 404 indicates that resource not fund. Can you cheek your request URL is right? Eg: http://localhost:8280/test/1.0
  • WSO2在云中具有API管理解决方案。 这是WSO2 API Cloud,它是WSO2 API Manager即服务。 您可以访问http://wso2.com/cloud/api-cloud/ ,注册并开始使用API Cloud。 您可以在https://docs.wso2.com/display/APICloud/WSO2+API+Cloud+Documentation找到API云的文档和教程。 WSO2 has a API Management solution in the cloud. Thats ...
  • 如果您只是想用OAuth2保护您的REST API,请使用WSO2 API Manager。 您可以按照快速入门指南开始。 有关添加API和启用OAuth2保护的详细信息,请参阅快速入门指南。 If you simply want to protect your REST API with OAuth2 use the WSO2 API Manager. You can follow the quick start guide to get started. Details related to addin ...
  • 虽然你的突触配置不存在,但我会回答你的问题。 你可以这两种方式。 一种是使用Header Mediator。 你可以提供这个 Doc。 示例代码如下,
    第二种方法是使用属性中介,您可以设置Header值并将范围设置为transport。 因此将添加Header属性。 您只需要将原始请求内容分配给属性,以便稍后使用。 有很多方法可以做到这一点,下面的例子是使用rich mediator ...
  • 云中有以下选项可用。 你仍然可以做IP白名单。 您可以请求将IP列入云支持频道白名单。 您可以使用API云和后端进行相互ssl 正如您所提到的,您可以发送一个自定义标题,其中包含一个来自API Cloud的秘密值,并从您的后端检查它 您也可以让API云将基本身份验证发送到您的后端 https://docs.wso2.com/display/APICloud/Secure+your+Backend+Services You have the following options available in the ...
  • 是的,WSO2 Identity Server功能安装在API Manager中。 如果您有一个单独运行的WSO2 Identity Server节点,它也可以充当密钥管理器 。 Yes, WSO2 Identity Server features are installed within API Manager. And if you have a WSO2 Identity Server node running separately, it can also act as the key manage ...
  • 我们发现了这个问题。 使用单字符参数定义URI参数存在问题。 作为解决方法,请使用不同的参数定义您的资源。 例如:/ client / calculate / add / {xx} / {yy} 此外,您必须将资源的“Produces”属性设置为“application / xml”,因为默认值为“application / json”,并且不受后端支持。 请参考下面的截图并定义您的API资源,并重新发布API。 We found the issue. There is a problem with def ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。