Computer Science, asked by lucky000001, 2 months ago

i is the imaginary unit. It is defined by i² = 1, therefore it is a solution to x² + 1 = 0.

Task:
→ Create a function p_of_i that returns i to the power a non-negative integer in its simplest form and as a string.

Example,
> pofi(0) = '1'
> pofi(1) = 'i'
> pofi(2) = '-1'
> pofi(3) = '-i'

I need one line program for this. Please help me out.
Please use Python only.

Answers

Answered by allysia
5

Sorry for the inconvenience before  i skipped 'one line program' part ^^"

Language:

Python

Program:

def p_of_i(n):

   return({0:1, 1:'i', 2:-1, 3:'-i', 4:1}[n%4] if n>0 else None)

'''

its a function doing something so 2 lines are minimum requirement.

You can run this without a function in single line as:

print({0:1, 1:'i', 2:-1, 3:'-i', 4:1}[n%4] if n>0 else None)

but then again you need at least one more line for user input I tried getting it in the same line but was getting this:

print({0:1, 1:'i', 2:-1, 3:'-i', 4:1}[int(input())%4] if int(input())>0 else None)

But you'll have to enter the same number twice it can be countered using a loop but it will get more none readable.

'''

Explanation:

  • Function returns nothing for negative power.
  • Returns 1 for power 0.
  • When the number is greater than 0 we check the returning value based on what is the remainder when divided by 4:
  1. When n is divisible by 4 we get remainder is 0 output will be 1
  2. If the remainder is 1 i is returned
  3. if it's 2 then -1 is returned.
  4. if non of above is then it must leave 3 remainder in that case we get -i, remainder 4 is not possibility since it is divisible 4 and case 1 will run again.
  • This is the logic compressed in one line. the keys are remainders. Condition ensures the input is positive.

Input output:

[In]:  print(p_of_i(19))

[Out]: -i

Attachment:

Attachments:
Answered by anindyaadhikari13
10

\texttt{\textsf{\large{\underline{Solution}:}}}

This is the simplest co‎de for the question -

It is written in Python.

p_of_i=lambda _:['1','i','-1','-i'][_%4]

\texttt{\textsf{\large{\underline{Explanation}:}}}

To solve this question, we have to know how to calculate i raised to the power a positive number.

Note:

→ i⁰ = 1

→ i¹ = i

→ i² = -1

→ i³ = -i

→ i⁴ = 1

In general,

→ i⁴ⁿ = 1

→ i⁴ⁿ⁺¹ = i

→ i⁴ⁿ⁺² = -1

→ i⁴ⁿ⁺³ = -i

When n is divided by four, possible remainders are - 0, 1, 2, 3

When it is 0: Result = 1

When it is 1: Result = i

When it is 2: Result = -1

When it is 3: Result = -i

Now, I have calculated n % 4 and acccessed the element of the list ['1','i','-1','-i'] at index n%4 which gives the desired result.

\texttt{\textsf{\large{\underline{Sample Outp{u}t}:}}}

The program -

p_of_i=lambda _:['1','i','-1','-i'][_%4]

for i in range(21):

   print('i to the power',i,'=',p_of_i(i))

Output -

i to the power 0 = 1

i to the power 1 = i

i to the power 2 = -1

i to the power 3 = -i

i to the power 4 = 1

i to the power 5 = i

i to the power 6 = -1

i to the power 7 = -i

i to the power 8 = 1

i to the power 9 = i

i to the power 10 = -1

i to the power 11 = -i

i to the power 12 = 1

i to the power 13 = i

i to the power 14 = -1

i to the power 15 = -i

i to the power 16 = 1

i to the power 17 = i

i to the power 18 = -1

i to the power 19 = -i

i to the power 20 = 1

See the attachment.

Attachments:
Similar questions