(ns src.helloworld)
(defn fibonacci[a b] (println a b (fibonacci (+ b 1) a + b)))
(fibonacci 0 1)
我是函数式编程的新手,我决定开始学习Clojure,因为它是,与C#完全不同。我想开阔眼界。
以下是我遇到的错误:
Clojure 1.2.0
java.lang.IllegalArgumentException:
Wrong number of args (4) passed to:
helloworld$fibonacci
(helloworld.clj:0) 1:1 user=>
#&
可能重复:
有人能在下面的代码中向我解释收益语句的实际作用吗:
def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a+b
关于fibonacci()中的数字:#使用生成器作为迭代器;打印编号
到目前为止,我所理解的是,我们正在定义一个没有参数的函数finonacci()?在函数中,我们定义了a和b,分别等于0和1,接下来,虽然这是真的,但是我们正在生成a。这到底是在做什么?此外,虽然产生a?a现在等于b,而b现在等于a + b。
下一个问题,对于fibonac
虽然这个程序告诉了真相(除非你改变源代码),但它告诉斐波纳契数字,取决于用户的输入。考虑到所有(或大部分)以前的问题的反馈,我写了这个程序,看看我有多好的进展。
def get_fibonacci_sequence(max):
'''
Returns the Fibonacci sequence from the
first to the nth Fibonacci number.
'''
fib1 = 1
fib2 = 1
result = [fib1, fib2]
for i
我正在为fibonacci系列做这个Clojure程序。
(def fibonacci []
(def a 0)
(def b 0)
(def c 1)
(def n (atom 0))
(while (<= @n 10)
(do
(def c (+ a b))
(def a b)
(def b c)
(println '(a b c))
)(swap! n inc)))
(fibonacci)
我得到了这个错误:
CompilerException java.lang.RuntimeExce
我正在学习Clojure。我对函数式编程相当陌生,我想知道我的代码是否有味道,或者我的方法是否对性能有任何影响。
; Returns the the given sequence with the given item appended to it.
(defn snoc [xs x] (concat xs [x]))
; Returns the Fibonacci sequence up to the highest number less than max.
(defn fib [max]
(loop [a 1, b 1, acc [1]]
(if (> b max
此代码应打印Fibonacci序列的前十个数字中的偶数和。
#Creates a list with the first ten Fibonacci numbers.
l = [1,2]
for i in range(10):
l.append(l[i]+l[i+1])
for i in l:
#If an element of the Fibonacci list is uneven, replace it with zero.
if l[i]%2 != 0:
l[i] = 0
#Print the sum of the list with al
如何在Fibonacci系列中使用Clojure atom来实现这一点。
(def x 0)
(def y 0)
(def z 1)
(defn problem []
(def a (atom 0))
(while (< @a 10 )
(do
;logic code need to write to achieve the output
; how can I add b,c == c,b???????.)
(swap! a inc)))
(problem)
产出应为0 1 1 2 3 5 8 13 21 34 55
给定Clojure表达式中的任意形式,我希望设计一个函数,该函数返回随机选择的相同类型签名的形式。
函数,我将命名为rand-form-sub,应该如下所示:
(rand-form-sub 2) => 8
(rand-form-sub 'or) => and
;; both and and or obey the same haskellian type signature:
;; f :: a -> a -> bool, where as are comparable for equality
(rand-form-sub '+) =>
我复制了别人在教程论坛上编写的python程序的源代码,并对其进行了一些修改以满足我自己的需要。
我相信原来的代码是在python shell中运行的,我让它在shell中运行,但我需要将它保存到python IDLE中,然后从那里运行它。
我使用的是python 3.2.3 IDLE btw,这是我写的:
def fibonacci(previous=0,current=1):
n = int(input("Calculate fibonacci sequence value up to: "))
if previous > current:
def fib_gen():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
print(next(fib_gen()))
print(next(fib_gen()))
print(next(fib_gen()))
print(next(fib_gen()))
Output: 0
0
0
0
我试图在python中创建一个无限的Fibonacci生成器。请帮帮我..。我哪里做错了?
我认为它的设计应该有更多的面向对象。我不喜欢我的一个方法如何从方法中调用另一个方法,但是我不确定如何返回结果,因为它是一个循环。
另外,是否可以让所有方法都是静态的,还是应该实例化类?
如果有任何其他改进,请告诉我,但这些是我的主要关切。
App.java:
public class App {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
Fibonacci.fibonacci();
long endTim
下面是我找到第n个斐波那契数的方法:
(defn fib-pair [[a b]]
"Return the next Fibonacci pair number based on input pair."
[b (+' a b)]) ; Use +' for automatic handle large numbers (Long -> BigInt).
(defn fib-nth [x]
"Return the nth Fibonacci number."
(nth (map first (iterate fib-
我为这个问题的简单性道歉,我已经搜索了很多次,但它可能是如此简单,以至于以前没有人问过它。
我写了一个Fibonacci函数,打印3000以下的每个Fibonacci数。
def fibonacci():
a, b = 0, 1
while b < 3000:
a, b = b, a + b
print a
return a
我怎么才能让它返回前n个斐波那契数呢?
另外,怎样才能让它只打印第n个值呢?例如print 6,它将返回8。我尝试将其设置为字符串:
a = str(fibonacci())
print a[6]
但这并不管用
我目前正在研究项目Euler问题#2,它要求您打印4百万以下的所有fibonacci数之和。
我最近才了解到生成器,根据我的理解,它们是用来通过动态生成值来减少内存消耗的(而不是存储在一个庞大的列表中)。
当在for循环中使用时,这是否仍然有效,还是只生成所有的值,将它们存储在一个列表中,然后遍历它们?
for n in some_generator(): pass
在我的具体案例中,代码如下所示:
def fibonacci(n): # yields all fibonacci numbers below n
second_last_n = 0
last_n = 1
def fibonacci_sequence():
a,b = 1,1
while True:
yield a
a,b = b, a+b
for i in range(10):
print(fibonacci_sequence().__next__())
我尝试在Python3中使用它来打印斐波那契数列。但程序最终只是一遍又一遍地打印1
我使用递归实现了fibonacci系列:
def fibonacci(n):
if n==0:
return 0
elif n==1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
我还使用动态编程实现了它:
def fibonacci(n):
result = [0, 1]
if n > 1:
for i in range(2, n+1):
result.append(result[i-
对于一次赋值,我们被要求定义一个斐波那契函数,我用下面的代码完成了这个函数:
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)
但是,我见过在单行返回语句中定义的递归函数,比如阶乘函数,如下所示:
def factorial(n):
return n > 1 and n * factorial(n-1) or 1
因此,我尝试将同样的方法应用于斐波那契函数。经过几次尝试,我让它在所有测试用例中都能正常工作,但当s=0时除外,在这种情况下,当它
我在工作中学习Perl,并且很享受它。我通常用Python完成工作,但老板需要Perl。
Python和Perl中的大多数概念都很好地匹配: Python dictionary=Perl散列;Python tuple=Perl列表;Python list=Perl数组;等等。
问:有没有 /生成器的Python形式的Perl版本?
例如:生成斐波那契数的经典Python方法是:
#!/usr/bin/python
def fibonacci(mag):
a, b = 0, 1
while a<=10**mag:
yield a
a
我正在尝试使用Fibonacci系列并获得该系列的多维数据集。我的Fibonacci函数提供了一个输出,但是我希望输出是一个列表形式。以下是我的代码
cube = lambda x : x ** 3
def fibonacci(n):
a = 0
b = 1
count = 0
if n < 0:
print("Incorrect input")
elif n == 0:
return a
elif n == 1:
return b
else:
我正在割我的牙齿在Akka流,并做了一个fibonacci出版商-订阅者的例子如下。然而,我还不太明白需求最初是如何产生的,它与订阅者的请求策略有什么关系。谁能解释一下吗?
FibonacciPublisher:
class FibonacciPublisher extends ActorPublisher[Long] with ActorLogging {
private val queue = Queue[Long](0, 1)
def receive = {
case Request(_) => // _ is the demand
log.debug
我想问一下如何检索出fibo列表的列表,然后检查用户的输入值是否在fibo列表中。
a , b = 1, 1
while num <= sys.maxint:
fibo == a , b = b, a+b
if num == (b +a+b):
print "It is a Fibonacci number"
break
else:
print "It is not a Fibonacci number"
break
谢谢!
我正在试着写一个程序,它将要求用户输入一个数字。然后我需要验证它是否在fib序列中。
代码:
# asking the user to input number
number = int(input("Enter a number: "))
# creating empty list to be filled
sequence = [0, 1]
x = -2
y = -1
# append items to list
for i in range(number):
x+=2
y+=2
# this code will be executed 'l