Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Question: The Fibonnaci sequence is a recursive sequence defined as: f0 =1,

ID: 3688440 • Letter: J

Question

Java Question:

The Fibonnaci sequence is a recursive sequence defined as:

f0 =1, f1 =1, fn =fn1 +fn2 So the first few terms are 1,1,2,3,5,8,13,21,34,55,....

Write a function/procedure/algorithm that computes the sum of all even-valued fibonnaci terms less than or equal to some positive integer k. For example the sum of all even-valued fibonnaci terms less than or equal to 40 is 2 + 8 + 34 = 44.

Please email me a program that I can compile and run, that solves the above problem given an input k as well as three test cases for three different input values. For example if you function is called evenfibonnacisum(k) then email me the evenfibonnacisum(k) file as well as three test cases such as evenfibonnacisum(40), evenfibonnacisum(400), evenfibonnacisum(4000).

Acceptable languages are C++, Java, Python, Matlab, Mathematica or Maple. My preference is Python or Mathematica.

Explanation / Answer

I am using python language. Python version 3

#Code start here

# define evenfibonnacisum funation here

def evenfibonnacisum(n):

   #Recursive function to get sequence

   if n <= 1:

       return n

   else:

       return(evenfibonnacisum(n-1) + evenfibonnacisum(n-2))

#input from the user

user_num = int(input("How many terms? "))

# check the number is valid and not

if user_num <= 0:

   print("Plese enter a positive integer")

else:

    total = 0

    count_msg =''

    for i in range(user_num):

        # call evenfibonnacisum function

       new_val = evenfibonnacisum(i)

        # check value 0 ot not

        if new_val > 0:

            # check current value grater then then break loop

            if(new_val > user_num):

                break

            else:

                #check even value

                if new_val % 2 == 0:

                    #sum with total

                    total += new_val

                    #check count_mdg null and not for print message Less than or equal to 40 is 2+8+34=44

                    if count_msg == '':

                        count_msg = str(new_val)

                    else:

                        count_msg += " + " + str(new_val)

               

               

#print final result

print("Less than or equal to "+str(user_num)+" is "+count_msg+" = "+str(total))

print("Sum of all even-valued:- "+ str(total))    

#Code end here

Output here

If user input is 40 then show result:-

Less than or equal to 40 is 2+8+34=44

Sum of all even-valued:- 44

If user input is 400 then show result:-

Less than or equal to 40 is 2+8+34+144=188

Sum of all even-valued:- 188

If user input is 400 then show result:-

Less than or equal to 40 is 2+8+34+144+610+2584=3382

Sum of all even-valued:- 3382

If user input is 8 then show result:-

Less than or equal to 40 is 2+8=10

Sum of all even-valued:- 10

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote