Tag Archive : python

Hofstadter Q-Sequence in Python

In this article, we have built a program to find the value of a number in the Q-Sequence (By Using Hofstadter Q-Sequence) with the Python code. Please install python 3.8 from the link given below to run this code.
https://www.python.org/downloads/

I prefer you to use atom text editor to type your code or to save the given code:
https://atom.io/

You can download the the Hofstadter Q-Sequence.py from GitHub using the given link:

Link for the code in GitHub

Example Input:

34

Output:

20

Code:

def Q(n):
    try:
        if n>0 and n<3:
           return 1
        elif n<0:
           return 'Invalid'
        else:
           return Q ( n - Q ( n - 1 ) ) + Q ( n - Q ( n - 2 ) )
    except:return "Enter an INTEGER value!"
print(Q(int(input())))

Code Explanation:

In this code, we get an input from the user which is the value of n in Q(n) .

Q ( 1 ) = Q ( 2 ) = 1 , Q ( n ) = Q ( n − Q ( n − 1 ) ) + Q ( n − Q ( n − 2 ) ) , n > 2

Hofstadter Q sequence Definition

Function Definition:

Here, we define a function that takes n as its parameter (input).
We use ‘try and ‘except’ in order to prevent unnecessary errors .

The error is caused only if the user enters anything other than float or int.

Here, we use Recursion in order to call back the same function to use Q ( n − Q ( n − 1 ) ) + Q ( n − Q ( n − 2 ) ) .

If the user enters a negative integer, the code outputs “Enter an INTEGER value!”

The base of the recursion is the condition “if n>0 and n<3:”
It returns 1 if the value becomes <3 (0,1 or 2).

Function Call:

At last, we call the function Q with int(input()) as parameter and print it out

Conclusion:

The above article explains the Hofstadter Q-Sequence using python coding. If you want to download the code from GitHub, click here. If you have any questions or feedback on this, feel free to post in the comments section below.

Quadratic Equation Code in Python

Introduction:

In this article, I am going to explain how to find the solutions for the quadratic equation with the Python code. Please install python 3.8 from the link given below to run this code.
https://www.python.org/downloads/

I prefer you to use atom text editor to type your code or to save the given code:
https://atom.io/

You can download the the Quadratic Equation.py from GitHub using the given link:

Link for the code in GitHub

Code:

#Quadratic equation
a=int(input())
b=int(input())
c=int(input())

d_d = b**2-(4*a*c) #Assigning variables to make the program simpler
d = d_d**(1/2)  #Root of b square -4ac
sol1 = ((-b)+d)/(2*a)
sol2 = ((-b)-d)/(2*a)

#Check if solution exists

if d_d < 0:
   print('Solution exists in complex numbers')

#Return the solution if it exists in real numbers) 
else:
   print('x = {0} or x = {1}'.format(sol1,sol2))  

Example Input:

1
2
1

Output:

x = -1.0 or x = -1.0

Code Explanation:

In this code, what we basically do is that, we get the 3 inputs from the user which are the values of a,b and c in a quadratic equation of the form ax2+bx+c.

Quadratic Equation in Python
Quadratic Equation

Code (Assignings):

  • The variable d_d is assigned the value of b2-4ac.
  • The variable d is assigned the value of root of d_d, that is root of b2-4ac.
  • Since an equation has 2 values ( ± ).
  • The value when used ‘+’ is assigned to the variable sol1.
  • Similarly, the value when used ‘-‘ is assigned to the variable sol2.

Code(If…Else):

  • In a quadratic equation when the value of b2-4ac is less than 0, then solution is in complex numbers.
  • In order to test this, we are checking whether d_d is less than 0 or not.
  • If less than 0, the ‘if’ statement is executed where the string (‘Solution exists in complex numbers’) is executed. If not then the real number value is executed (else statement)

Conclusion:

The above article explains the solution to quadratic equation using python coding. If you have any questions or feedback on this, feel free to post in the comments section below.