Question

I want a python code using recursion ONLY. I cannot use any loops, dictionaries or modules....

I want a python code using recursion ONLY.

I cannot use any loops, dictionaries or modules.

Only math module is allowed.

I want a function that creates a list of the first N prime numbers:

for eg create_primes(2) = [2,3]

create_primes(5) = [2,3,5,7,11]

def create_primes(N):

pass

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import sys
sys.setrecursionlimit(5000) # set the recusion limit by default it is 1000
list=[] # store the N prime numbers
k=2
count=2
# method to check the given number is prime or not
def isprime(n,count):
if count==n:
return True
elif n%count==0:
return False
else:
return isprime(n,count+1)
# method return the list containing the first N prime numbers
def create_primes(N):
global list,k
if N==0:
return list
else:
if isprime(k,count):
list.append(k)
k+=1
return create_primes(N-1)
else:
k+=1
return create_primes(N)
# Take input from users
N=int(input("Enter the Number "))
#printing the prime number list
print("The First ",N," prime numbers are ---->",create_primes(N))
  
  

Add a comment
Know the answer?
Add Answer to:
I want a python code using recursion ONLY. I cannot use any loops, dictionaries or modules....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE do not use any loops for the program; only recursion is allowed 4. Write a...

    PLEASE do not use any loops for the program; only recursion is allowed 4. Write a Python function to print all prime numbers that are less than or equal to some number n. (Do not use Python lists) (15pts)

  • CODE IN PYTHON 2.7: USE GENERATORS The Fibonacci numbers are defined by the following recursion: with...

    CODE IN PYTHON 2.7: USE GENERATORS The Fibonacci numbers are defined by the following recursion: with initial values. Using generators, compute the first ten Fibonacci numbers, [1,1,2,3,5, 8,13,21,34,55] def fibonacci(n): F, = Fn-1 + Fn-2 In # YOUR CODE HERE raise NotImplementedError)

  • Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. A. Write a...

    Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. A. Write a function named smaller that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all numbers in the list that are smaller than the number n. Write a code that declares and initializes a list of numbers and a variable number and pass them to the function to test it. Please see the outcome below:...

  • You must use recursion to solve each problem. You cannot use loops in this homework. You...

    You must use recursion to solve each problem. You cannot use loops in this homework. You cannot import any module. A couple of the tasks have individual restrictions; note them, as we will remove points for any task that does not follow the requirements. def factorial_evens(num): Implement a function to calculate and return the product of all even numbers from 1 up to num (inclusive if num is even). o Assumption: num will be an integer greater than or equal...

  • Unrolling Recursion The objective of this problem is to simulate recursion using stacks and loops...

    Unrolling Recursion The objective of this problem is to simulate recursion using stacks and loops. A synthetic linear recursive procedure for this problem is provided in Code Fragment 1. A recursive function such as the one described is intuitive and easily understandable. On calling myRecursion(input), the execution first checks for the stopping condition. If the stopping condition is not met, then operations inside the recursive call are performed. This can include operations on local variables. The operations inside the function...

  • USING ONLY LOOPS CANNOT USE INDEXING OF STRINGS (STR) PYTHON 3 CODE IS TESTED WITH JDK...

    USING ONLY LOOPS CANNOT USE INDEXING OF STRINGS (STR) PYTHON 3 CODE IS TESTED WITH JDK AND WILL KNOW WHEN USING FORBIDDEN CODE write a program called reversibleSentence -- ask for a word from user -- if word is the same read from left to right or right to left, then -- print "the word is reversible", else -- print "the word IS NOT reversible" HINT: The following program prints heo sentence = "" for x in "hello": if x...

  • See the image below and write the code using C++, without using variables, only recursion. 3....

    See the image below and write the code using C++, without using variables, only recursion. 3. 5 a [DO NOT USE ANY VARIABLES Write a C++ function that takes two integer parameters (a and b) and prints a list of all the integers between a and b-1 (inclusive). one (2, 12) should print 2 34567 8 9 10 11. one (2, 13) should print 2 34567 8 9 10 11 12 b. [D NOT USE ANY VARIABLES Write a C++...

  • Python code: using only len(), range(), append(), and del keyword when needed as the directions state?...

    Python code: using only len(), range(), append(), and del keyword when needed as the directions state? No list comprehension, slicing, or importing modules. Please and thank you. def diagonal_matrix(mat) Converts a square matrix mat into a lower diagonal matrix https://en.wikipedia.org/wiki/Triangular_matrix After the conversion the values on the diagonal will be the sum of the deleted elements in the respective row Return value is None. Variable mat is directly modi ed in memory mat is a nested list (e.g. [[1,2,3], [4,5,6],...

  • In python, please help me fill in these functions given their limitations. Please use for loops....

    In python, please help me fill in these functions given their limitations. Please use for loops. def palindrome(x): """ For a given string, determine if the string is a palindrome. (The same forward and back) Input: String of any size (including empty) Return: Boolean (True if palindrome, False otherwise) Limitation: You must use a loop. Only function allowed to be used is len (if needed). Cannot use "in" besides "for var in container" """ pass def getCount(char, strng): """ Get...

  • This problem demonstrates the use of import module. The Python programming language has many strengths, but...

    This problem demonstrates the use of import module. The Python programming language has many strengths, but one of its best is the availability to use many existing modules for various tasks, and you do not need to be an experienced computer programmer to start using these modules. We have given you some incomplete code; note that the very first line of that code contains an import statement as follows: import math This statement enables your program to use a math...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT