Juggler Sequence is a series of integers in which the first term starts with a positive integer number a and the remaining terms are generated from the immediate previous term using the below recurrence relation:
Given a number N, find the Juggler Sequence for this number as the first term of the sequence.
Example 1:
Input: N = 9
Output: 9 27 140 11 36 6 2 1
Explaination: We start with 9 and use 
above formula to get next terms.
 
Example 2:
Input: N = 6
Output: 6 2 1
Explaination: 
6^{1/2} = 2. 
2^{1/2} = 1.
 
Your Task:
You do not need to read input or print anything. Your Task is to complete the function jugglerSequence() which takes N as the input parameter and returns a list of integers denoting the generated sequence.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
 
Constraints:
1 ≤ N ≤ 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Complete search

Starter code: #User function Template for python3



class Solution:

    def jugglerSequence(self, N):

        # code here

Solutions:
import math

class Solution:

	def jugglerSequence(self, N):
		if N == 1:
			return [1]
		seq = [N]
		if N % 2 == 0:
			seq.extend(self.jugglerSequence(int(N ** 0.5)))
		else:
			seq.extend(self.jugglerSequence(int(N ** 1.5)))
		return seq


class Solution:

	def jugglerSequence(self, N):
		self.ans = [N]

		def recursion(N):
			if N == 1:
				return
			if N % 2 == 0:
				N = int(pow(N, 0.5))
			else:
				N = int(pow(N, 1.5))
			self.ans.append(N)
			recursion(N)
		recursion(N)
		return self.ans


import math

class Solution:

	def jugglerSequence(self, N):
		res = [N]
		while N > 1:
			if N % 2:
				N = int(math.sqrt(N) * N)
			else:
				N = int(math.sqrt(N))
			res.append(N)
		return res


import math

class Solution:

	def sol(self, prev, count, ans):
		curr = prev
		if curr != 1:
			if prev % 2 == 0:
				curr = int(prev ** (1 / 2))
			else:
				curr = int(prev ** (3 / 2))
			ans.append(curr)
			self.sol(curr, count + 1, ans)
		return ans

	def jugglerSequence(self, N):
		ans = []
		ans.append(N)
		return self.sol(N, 2, ans)


class Solution:

	def jugglerSequence(self, N, list=None):
		m = N
		if list == None:
			list = [m]
		elif N > 0 and N != 1:
			if N % 2 == 0:
				m = int(pow(N, 0.5))
				list.append(m)
			else:
				m = int(pow(N, 1.5))
				list.append(m)
		else:
			return list
		list = self.jugglerSequence(m, list)
		return list


class Solution:

	def jugglerSequence(self, N):

		def jug(N, lis):
			if N == 1:
				lis.append(1)
				return lis
			elif N % 2 == 1:
				lis.append(N)
				a = int(pow(N, 1.5))
				return jug(a, lis)
			else:
				lis.append(N)
				a = int(pow(N, 0.5))
				return jug(a, lis)
		lis = []
		return jug(N, lis)


class Solution:

	def jugglerSequence(self, N):

		def jug(lis, n):
			if n == 1:
				lis.append(n)
				return lis
			if n & 1 == 0:
				lis.append(n)
				return jug(lis, int(n ** 0.5))
			else:
				lis.append(n)
				return jug(lis, int(n ** 1.5))
		lis = []
		return jug(lis, N)


import math

class Solution:

	def helper(self, N, seq):
		if N == 1:
			return 1
		elif N % 2 == 0:
			N = math.floor(N ** (1 / 2))
		elif N % 2 == 1:
			N = math.floor(N ** (3 / 2))
		self.helper(N, seq)
		seq.append(N)

	def jugglerSequence(self, N):
		seq = []
		self.helper(N, seq)
		seq.append(N)
		return seq[::-1]


class Solution:

	def jugglerSequence(self, N):
		arr = []

		def recurse(m):
			if m == 1:
				arr.append(1)
				return arr
			elif m % 2 == 0:
				arr.append(m)
				recurse(int(m ** 0.5))
				return arr
			else:
				arr.append(m)
				recurse(int(m ** 1.5))
				return arr
		return recurse(N)


class Solution:

	def jugglerSequence(self, N):
		seq = [N]
		while N > 1:
			if N % 2 == 0:
				N = int(N ** 0.5)
			else:
				N = int(N ** 1.5)
			seq.append(N)
		return seq


import math

class Solution:

	def jugglerSequence(self, N):
		ans = []
		ans.append(N)

		def rec(v):
			if v == 1:
				return
			if ans[-1] % 2 == 0:
				ans.append(math.floor(ans[-1] ** 0.5))
			else:
				ans.append(math.floor(ans[-1] ** 1.5))
			rec(ans[-1])
		rec(N)
		return ans


class Solution:

	def jugglerSequence(self, N):
		arr = []
		if N == 1:
			return [1]
		elif N % 2 == 0:
			arr += [N]
			arr += self.jugglerSequence(int(N ** 0.5))
			return arr
		else:
			arr += [N]
			arr += self.jugglerSequence(int(N ** 1.5))
			return arr
		return arr


class Solution:

	def jugglerSequence(self, N):
		l = [N]

		def rec(N):
			if N == 1:
				return
			if N % 2 == 0:
				N = int(N ** 0.5)
				l.append(N)
			else:
				N = int(N ** 1.5)
				l.append(N)
			rec(N)
		rec(N)
		return l


class Solution:

	def jugglerSequence(self, N):
		res = []
		return self.dummy(N, res)

	def dummy(self, N, res):
		res.append(N)
		if N == 1:
			return res
		if N % 2 == 0:
			return self.dummy(int(pow(N, 1 / 2)), res)
		else:
			return self.dummy(int(pow(N, 3 / 2)), res)


class Solution:

	def jugglerSequence(self, N):
		ans = []
		self.helper(N, ans)
		return ans

	def helper(self, num, ans):
		ans.append(num)
		if num == 1:
			return ans
		if num % 2 == 0:
			num = int(num ** 0.5)
		else:
			num = int(num ** 1.5)
		return self.helper(num, ans)


import math

class Solution:

	def __init__(self):
		self.result = []

	def jugglerSequence(self, N):
		self.result.append(N)
		if N != 1:
			if N % 2 == 0:
				N = math.floor(pow(N, 1 / 2))
				return self.jugglerSequence(N)
			else:
				N = math.floor(pow(N, 3 / 2))
				return self.jugglerSequence(N)
		return self.result


import math

class Solution:

	def jugglerSequence(self, N):
		self.N = N
		curr = N
		result = []
		result.append(N)
		while curr != 1:
			newNum = 0
			if curr % 2 == 0:
				newNum = math.floor(pow(curr, 1 / 2))
				result.append(newNum)
				curr = newNum
			else:
				newNum = math.floor(pow(curr, 3 / 2))
				result.append(newNum)
				curr = newNum
		return result


import math

class Solution:

	def jugglerSequence(self, N):
		res = []
		self.helper(N, res)
		return res

	def helper(self, num, res):
		res.append(num)
		if num == 1:
			return res
		if num % 2 == 0:
			num = int(pow(num, 0.5))
		else:
			num = int(pow(num, 1.5))
		return self.helper(num, res)


class Solution:

	def jugglerSequence(self, N):
		lst = [N]
		while lst[-1] != 1:
			if lst[-1] % 2 == 0:
				lst.append(int(lst[-1] ** (1 / 2)))
			else:
				lst.append(int(lst[-1] ** (3 / 2)))
		return lst


class Solution:

	def jugglerSequence(self, N):
		import numpy as np
		seq = []
		while N > 1:
			seq += [N]
			if N % 2 == 0:
				N = int(np.floor(np.sqrt(N)))
			else:
				N = int(np.floor(np.power(N, 1.5)))
		seq += [1]
		return seq


import math

class Solution:

	def jugglerSequence(self, n):
		b = []
		b.append(n)
		if n == 1:
			return b
		i = 1
		while b[i - 1] != 1:
			if b[i - 1] % 2 == 0:
				b.append(math.floor(pow(b[i - 1], 0.5)))
			else:
				b.append(math.floor(pow(b[i - 1], 1.5)))
			i += 1
		return b


class Solution:

	def jugglerSequence(self, N):
		l = []
		k = N
		l.append(N)
		i = 1
		while l[-1] != 1:
			if l[i - 1] % 2 == 0:
				l.append(int(l[i - 1] ** (1 / 2)))
				i += 1
			else:
				l.append(int(l[i - 1] ** (3 / 2)))
				i += 1
		return l


import math

class Solution:

	def juggler(self, n, ans):
		if n == 1:
			return ans
		if n % 2 == 0:
			n = int(math.sqrt(n))
			ans.append(n)
		else:
			n = int(n ** 1.5)
			ans.append(n)
		self.juggler(n, ans)
		return ans

	def jugglerSequence(self, N):
		return self.juggler(N, [N])


class Solution:

	def jugglerSequence(self, N):
		k = [N]
		while N > 1:
			if N % 2 == 0:
				k.append(int(N ** 0.5))
				N = int(N ** 0.5)
			elif N % 2 == 1:
				k.append(int(N ** (3 / 2)))
				N = int(N ** (3 / 2))
		return k


import math

class Solution:

	def jugglerSequence(self, N):
		k = [N]
		i = 0
		while k[i] >= 0:
			if k[i] == 1:
				break
			elif k[i] % 2 == 0:
				k.append(int(math.sqrt(k[i])))
				i += 1
			else:
				k.append(int(math.pow(k[i], 3 / 2)))
				i += 1
		return k


class Solution:

	def jugglerSequence(self, N):
		l = [N]
		while l[-1] > 1:
			if l[-1] % 2 == 0:
				x = int(l[-1] ** 0.5)
				l.append(x)
			else:
				x = int(l[-1] ** 1.5)
				l.append(x)
		return l


import math

class Solution:

	def solve(self, N, out, ans):
		if N <= 1:
			ans += out
			return ans
		if N % 2 == 0:
			k = math.pow(N, 1 / 2)
		else:
			k = math.pow(N, 3 / 2)
		N = int(k)
		out.append(N)
		self.solve(N, out, ans)

	def jugglerSequence(self, N):
		ans = [N]
		out = []
		self.solve(N, out, ans)
		return ans


class Solution:

	def jugglerSequence(self, N):
		l = []

		def jug(N):
			if N == 1:
				l.append(N)
				return
			l.append(N)
			if N % 2 == 0:
				k = int(N ** 0.5)
			else:
				k = int(N ** 1.5)
			jug(k)
		jug(N)
		return l


def seq(n, arr):
	arr.append(n)
	if n <= 1:
		return
	if n % 2 == 0:
		n = int(pow(n, 1 / 2))
		seq(n, arr)
	else:
		n = int(pow(n, 3 / 2))
		seq(n, arr)
	return arr

class Solution:

	def jugglerSequence(self, N):
		arr = []
		seq(N, arr)
		return arr


import math

class Solution:

	def solution(self, N, lst):
		lst.append(N)
		if N == 1:
			return lst
		if N % 2 == 1:
			x = int(N ** 1.5)
			return self.solution(x, lst)
		else:
			x = int(math.sqrt(N))
			return self.solution(x, lst)

	def jugglerSequence(self, N):
		arr = []
		return self.solution(N, arr)


class Solution:

	def jugglerSequence(self, N):
		L = [N]

		def g(N, L):
			if N == 1:
				return 1
			if N % 2 == 0:
				N = int(N ** 0.5)
			elif N % 2 != 0:
				N = int(N ** 1.5)
			L.append(N)
			N = g(N, L)
			return N
		g(N, L)
		return L


class Solution:

	def jugglerSequence(self, N):
		arr = [N]
		x = 0
		while x == 0:
			if N % 2 == 0 and N != 1:
				N = int(N ** 0.5)
				arr.append(N)
			elif N % 2 != 0 and N != 1:
				N = int(N ** 1.5)
				arr.append(N)
			if N == 1:
				x += 1
				break
		return arr


class Solution:

	def jugglerSequence(self, n):
		a = []
		a.append(n)
		while n > 1:
			if n % 2 == 0:
				n = int(pow(n, 0.5))
				a.append(n)
			else:
				n = int(pow(n, 1.5))
				a.append(n)
		return a


class Solution:

	def jugglerSequence(self, n):
		a = []
		while n != 1:
			if n % 2 == 0:
				a.append(n)
				n = int(n ** (1 / 2))
			else:
				a.append(n)
				n = int(n ** (3 / 2))
		a.append(1)
		return a


import math

class Solution:

	def jugglerSequence(self, N):
		l = []
		l.append(N)
		while N > 1:
			N = math.floor(math.pow(N, 0.5)) if N % 2 == 0 else math.floor(math.pow(N, 1.5))
			l.append(N)
		return l


class Solution:

	def jugglerSequence(self, N):
		l = []
		l.append(N)
		while N > 1:
			if N % 2 == 1:
				l.append(int(pow(N, 3 / 2)))
				N = int(pow(N, 3 / 2))
			else:
				l.append(int(pow(N, 1 / 2)))
				N = int(pow(N, 1 / 2))
		return l


class Solution:

	def jugglerSequence(self, N):
		arr = [N]
		while arr[-1] != 1:
			if arr[-1] % 2:
				arr.append(int(arr[-1] ** (3 / 2)))
			else:
				arr.append(int(arr[-1] ** (1 / 2)))
		return arr


class Solution:

	def jugglerSequence(self, N):
		a = []
		self.sequence(N, a)
		return a

	def sequence(self, N, a):
		a.append(N)
		if N == 1:
			return a
		if N % 2 == 0:
			p = int(N ** 0.5)
			return self.sequence(int(N ** 0.5), a)
		else:
			p = int(N ** 1.5)
			return self.sequence(int(N ** 1.5), a)


class Solution:

	def jugglerSequence(self, n):
		l = [n]
		p = n
		while p != 1:
			if p % 2:
				k = int(p ** (3 / 2))
				l.append(k)
				p = k
			elif p % 2 == 0:
				m = int(p ** (1 / 2))
				l.append(m)
				p = m
		return l


import math

def jaggu(N, ans):
	if N == 1:
		return
	elif N % 2 == 0:
		c = math.floor(math.sqrt(N))
		ans.append(c)
		jaggu(c, ans)
	else:
		k = N * N * N
		c = math.floor(math.sqrt(k))
		ans.append(c)
		jaggu(c, ans)

class Solution:

	def jugglerSequence(self, N):
		ans = [N]
		jaggu(N, ans)
		return ans


class Solution:

	def jugglerSequence(self, N):
		ans = []
		ans.append(N)
		while N >= 2:
			if N % 2 == 0:
				v = int(N ** (1 / 2))
				ans.append(v)
				N = v
			else:
				v = int(N ** (3 / 2))
				ans.append(v)
				N = v
		return ans


import math

class Solution:

	def jugglerSequence(self, N):
		l = [N]
		while N != 1:
			if N % 2 == 0:
				N = math.floor(N ** (1 / 2))
				l.append(N)
			elif N % 2 != 0:
				N = math.floor(N ** (3 / 2))
				l.append(N)
		return l


class Solution:

	def jugglerSequence(self, N):
		l = [N]
		if N == 1:
			return l
		if N % 2 == 0:
			N = int(pow(N, 1 / 2))
			smalleroutput = self.jugglerSequence(N)
			l.extend(smalleroutput)
			return l
		else:
			N = int(pow(N, 3 / 2))
			smalleroutput = self.jugglerSequence(N)
			l.extend(smalleroutput)
			return l
		return l


import math

class Solution:

	def jugglerSequence(self, N):
		ans = []
		self.help(N, ans)
		return ans

	def help(self, n, ans):
		ans.append(n)
		if n == 1:
			return ans
		if n % 2 == 0:
			n = int(pow(n, 0.5))
		else:
			n = int(pow(n, 1.5))
		return self.help(n, ans)


class Solution:

	def jugglerSequence(self, n):
		arr = [n]

		def jugglerSequenc(n):
			if n == 1:
				return
			if n % 2 == 1:
				val = int(n ** (3 / 2))
				arr.append(val)
				jugglerSequenc(val)
			else:
				val = int(n ** (1 / 2))
				arr.append(val)
				jugglerSequenc(val)
		jugglerSequenc(n)
		return arr


class Solution:

	def jugglerSequence(self, N):
		ans = [N]
		while ans[-1] != 1:
			a = ans[-1]
			if a % 2 == 0:
				ans.append(int(a ** 0.5))
			else:
				ans.append(int(a ** 1.5))
		return ans


class Solution:

	def func(self, n, ans):
		ans.append(n)
		if n == 1:
			return ans
		if n % 2 == 0:
			n = int(n ** 0.5)
		else:
			n = int(n ** 1.5)
		self.func(n, ans)

	def jugglerSequence(self, N):
		ans = []
		self.func(N, ans)
		return ans


class Solution:

	def __init__(self):
		self.ans = list()

	def jugglerSequence(self, N):
		self.ans.append(N)
		if N > 1:
			if N % 2 == 0:
				N = int(N ** (1 / 2))
				return self.jugglerSequence(N)
			else:
				N = int(N ** (3 / 2))
				return self.jugglerSequence(N)
		else:
			return self.ans


import math

class Solution:

	def jugglerSequence(self, n):
		arr = [n]
		while n != 1:
			if n % 2 == 1:
				n = math.sqrt(pow(n, 3))
			else:
				n = math.sqrt(n)
			n = int(n)
			arr.append(n)
		return arr


class Solution:

	def jugglerSequence(self, N):
		ans = []

		def sequence(N):
			ans.append(N)
			if N == 1:
				return
			if N % 2 == 0:
				z = int(N ** 0.5)
				return sequence(z)
			else:
				z = int((N ** 3) ** 0.5)
				return sequence(z)
		sequence(N)
		return ans


class Solution:

	def jugglerSequence(self, N):

		def g(N):
			if N == 1:
				return 1
			if N % 2 == 0:
				return int(N ** 0.5)
			else:
				return int(N ** 1.5)
		l = [N]
		for a in l:
			if N == 1:
				break
			l.append(g(a))
			if g(a) == 1:
				break
		return l


class Solution:

	def jugglerSequence(self, N):
		arr = []
		arr.append(N)
		self.juggle(N, arr)
		return arr

	def juggle(self, N, arr):
		if N == 1:
			return arr
		if N % 2 == 0:
			num = int(pow(N, 1 / 2))
		else:
			num = int(pow(N, 3 / 2))
		arr.append(num)
		self.juggle(num, arr)


class Solution:

	def jugglerSequence(self, N):
		res = [1]
		if N == 1:
			return res

		def jugglerSeq(n):
			if n == 1:
				return
			if n % 2 == 0:
				jugglerSeq(int(n ** 0.5))
			else:
				jugglerSeq(int(n ** 1.5))
			res.append(n)
			return res[::-1]
		return jugglerSeq(N)


import math

class Solution:

	def jugglerSequence(self, N):
		res = []

		def seq(N):
			res.append(N)
			if N == 1:
				return
			if N % 2 == 0:
				return seq(int(math.sqrt(N)))
			else:
				return seq(int(math.sqrt(N) ** 3))
		seq(N)
		return res


class Solution:

	def jugglerSequence(self, N):

		def solve(N, arr):
			import math
			arr.append(N)
			if N == 1:
				return arr
			if N % 2 == 0:
				N = int(math.sqrt(N))
			else:
				N = int(math.sqrt(math.pow(N, 3)))
			return solve(N, arr)
		return solve(N, [])


class Solution:

	def jugglerSequence(self, N):
		ans = []
		self.helper(ans, N, N)
		return ans

	def helper(self, ans, k, N):
		if k == 1:
			ans.append(k)
			return
		elif k & 1:
			ans.append(k)
			self.helper(ans, int(k ** (3 / 2)), N)
		else:
			ans.append(k)
			self.helper(ans, int(k ** (1 / 2)), N)


class Solution:

	def jugglerSequence(self, N: int) -> list:
		result = []
		while N != 1:
			result.append(N)
			if N % 2 == 0:
				N = int(N ** 0.5)
			else:
				N = int(N ** 1.5)
		result.append(1)
		return result


import math

class Solution:

	def jugglerSequence(self, N):
		res = []
		res.append(N)
		while N != 1:
			if N % 2 != 0:
				N = math.floor(N ** (3 / 2))
				res.append(N)
			else:
				N = math.floor(N ** (1 / 2))
				res.append(N)
		return res


import math

class Solution:

	def jugglerSequence(self, N):
		seq = []

		def recur(n):
			seq.append(n)
			if n == 1:
				return
			if n % 2 == 0:
				next = int(math.pow(n, 0.5))
				recur(next)
			else:
				next = int(math.pow(n, 1.5))
				recur(next)
		recur(N)
		return seq