Question

In: Computer Science

Python #Remember that Fibonacci's sequence is a sequence of numbers #where every number is the sum...

Python

#Remember that Fibonacci's sequence is a sequence of numbers
#where every number is the sum of the previous two numbers.
#
#Joynernacci numbers are similar to Fibonacci numbers, but
#with two differences:
#
# - Fibonacci numbers are famous, Joynernacci numbers are
# not (yet).
# - In Joynernacci numbers, even-indexed numbers are the
# sum of the previous two numbers, while odd-indexed
# numbers are the absolute value of the difference
# between the previous two numbers.
#
#For example: the Joynernacci sequence starts with 1 and 1
#as the numbers at index 1 and 2. 3 is an odd index, so
#the third number would be 0 (1 - 1 = 0). 4 is an even
#index, so the fourth number would be 1 (0 + 1). 5 is an
#odd index, so the fifth number would be 1 (1 - 0). And
#so on.
#
#The first several Joynernacci numbers (and their indices)
#are thus:
#
# 1 1 0 1 1 2 1 3 2 5 3 8 5 13 8 21 13 34 21
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
#Write a function called joynernacci that returns the nth
#Joynernacci number. For example:
#
# joynernacci(5) -> 1
# joynernacci(12) -> 8
#
#We recommend implementing joynernacci recursively, but it
#is not required.


#Write your code here!


#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print: 1, then 8
print(joynernacci(5))
print(joynernacci(12))

Solutions

Expert Solution

The function joyernacci can be defined as follows:

  • Use def joyernacci(n) as the function signature. n is the index of the term that has to be printed.
  • Create an empty list.
  • Append 0, 1 and 1 to the list.
  • Use a for loop, which will start at position 3 until n+1.
  • For the term whose index is even, the value is addition of previous two terms.
  • For the term whose index is odd, the value is the absolute value of the difference of previous two terms.
  • Print the nth term of the list

The python program:

#function to return the nth joynernacci term
def joynernacci(n):
    l = []
    l.append(0)    #0 at 0th index
    l.append(1)    #1 at 1st index
    l.append(1)    #2 at 2nd index
    
    #computing terms for index 3 to n
    for i in range(3,n+1):
        #for even terms
        if (i)%2==0:
            l.append(l[i-1] + l[i-2])
        #for odd terms
        else:
            l.append(abs((l[i-1]) - l[i-2]))
      
    #print the nth 
    print(l[n])
        
#call function for 5th term
joynernacci(5)

#call function for 12th term
joynernacci(12)

Here is the code sippet to understand the indentation:

Output:

Code in jupyter with output:


Related Solutions

use Python The assertion that every even number is the sum of two prime numbers is...
use Python The assertion that every even number is the sum of two prime numbers is called Goldbach’s conjecture. You will write a program that asks the user for an integer number, then checks if the integer is even, and finally determines two prime numbers that sum to the user’s entered integer number. Assignment Requirements Three functions are required: get_input(): This function takes no parameters, but will ask the user for an integer number. It will return a valid integer....
A Fibonacci sequence, is a sequence of numbers with the property that each number is the...
A Fibonacci sequence, is a sequence of numbers with the property that each number is the sum of the two preceding Fibonacci numbers, starting from 0 and 1. Fibonacci number are usually denoted by Fn, where Fn is the nth Fibonacci number. Thus Fn = 0, and Fn = 1, and Fn = Fn-1 + Fn-2, n ≥ 2. Here are the first few Fibonacci numbers: F0=0 (by definition) F1=1 (by definition) F2 = F1 + F0 = 1 +...
python: ask the user to input a sequence of positive numbers. the end of the sequence...
python: ask the user to input a sequence of positive numbers. the end of the sequence is determined when the user enters a negative number. print out the maximum number from the sequence. output: keep entering positive numbers. to quit, input a negative number. enter a number: 67 enter a number: 5 enter a number: 8 enter a number: -3 largest number entered: 67 (note: i do not want to ask the user how many numbers they will input)
python def create_fourier_dataset(x, max_val=7): """ Return the sum of sin(n*x)/n where n = 1,2,3,4,5... max_val Remember,...
python def create_fourier_dataset(x, max_val=7): """ Return the sum of sin(n*x)/n where n = 1,2,3,4,5... max_val Remember, n is a scalar quantity (float/int). x is a NumPy array and you should return an equal length NumPy array :param x: numpy array :param max_val: The maximum value :return: a tuple with two numpy arrays x and the sum """
(Python) Implement a function to compute a sum that can compute sum for an arbitrary number...
(Python) Implement a function to compute a sum that can compute sum for an arbitrary number of input integers or float numbers. In other words, calls like this should all work: flexible_sum(x1, x2) # sum of two integer or float numbers or strings x1 and x2 flexible_sum(x1, x2, x3) # sum of 3 input parameters and can also handle a single input parameter, returning it unchanged and with the same type, i.e.:   flexible_sum(1) returns 1 and can accept an arbitrary...
Please use C++ 1. A Sequence of numbers and a sum is given as input. List...
Please use C++ 1. A Sequence of numbers and a sum is given as input. List all combinations of numbers that can add upto the given Sum. User input:- Enter numbers: -   1, 3,7,9,11 Enter a sum :- 20 Output: 11+9 = 20 1+3+7+9 = 20 2. Print absolute prime numbers between a given range:- Enter Range - 2,99 For eg Prime numbers are:- Prime numbers ==> 2,3,5,7,11,13,17,19,23 Absolute Prime numbers ==> 2,3,5,7,11,23 3. Write an encoder and decoder program,...
Write a python program to sum the prime numbers existing in an array . For instance...
Write a python program to sum the prime numbers existing in an array . For instance , if A = [4, 7, 12, 3, 9] the output should be 10
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Using Python: The Fibonacci sequence is a famous series of numbers with the following rules: The...
Using Python: The Fibonacci sequence is a famous series of numbers with the following rules: The first number in the sequence is 0 - The second number in the sequence is 1 - The other numbers in the sequence are composed by adding up the two previous numbers in the sequence. We therefore have the following sequence: 1 st number: 0 2nd number: 1 3 rd number: 0 + 1 = 1 4 th number: 1+1 =2 5 th number:...
python coding Suppose a list of positive numbers is given like the following list (remember this...
python coding Suppose a list of positive numbers is given like the following list (remember this is only an example and the list could be any list of positive numbers) exampleList: 15 19 10 11 8 7 3 3 1 We would like to know the “prime visibility” of each index of the list. The “prime visibility” of a given index shows how many numbers in the list with indexes lower than the given index are prime. For instance, in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT