Question

convert this program to python to see it this program is valid or not (* ported...

convert this program to python to see it this program is valid or not

(*
   ported by Eugene Wallingford from a program written
   in another language by former student Andrew Howard

   A circular prime is a prime number all of which's cyclical
   permutations are prime.  For example, 37 is a circular prime,
   because 73 is also prime.  113 is, too, because 131 and 311
   are prime.

   https://en.wikipedia.org/wiki/Circular_prime
*)

function main(x:integer):integer
  circularPrimesTo(x)

function circularPrimesTo(x:integer):integer
  circularPrimesToHelper(x+1, 2, 0)

function circularPrimesToHelper(top:integer, x:integer, count:integer):integer
  if x < top then
     if isCircularPrime(x) then
        circularPrimesToHelper(top, x+1, count+1)
     else
        circularPrimesToHelper(top, x+1, count)
  else
     count

function isCircularPrime(x:integer):boolean
  if isCircularPrimeHelper( x, log10(x)+1 ) then
     report(x)
   else
     false

function isCircularPrimeHelper(x:integer, turns:integer):boolean
  if turns = 0 then
     true
  else
     isPrime(x) and isCircularPrimeHelper( rotate(x), turns-1 )

function report(x:integer):boolean
  print(x)
  true

function rotate(x:integer):integer
  x/10 + ( mod(x,10) * pow(10, log10(x)) )

function pow(x:integer,y:integer):integer
  powHelper(x,y,x)

function powHelper(base:integer, power:integer, total:integer):integer
  if power = 0 then
     1
  else if power = 1 then
     total
  else
     powHelper(base, power-1, base*total)

function log10(x:integer):integer
  log10Helper(x,0)

function log10Helper(x:integer, y:integer):integer
  if (x / 10) = 0 then
     y
  else
     log10Helper( x/10, y+1 )

(* -----  code blatantly stolen from sieve  ----- *)

function isPrime( n : integer ) : boolean
  not hasDivisorFrom(2, n)

function hasDivisorFrom( i : integer, n : integer) : boolean
  if i < n then
     divides(i, n) or hasDivisorFrom(i+1, n)
  else
     false

function divides( a : integer, b : integer) : boolean
  mod(b, a) = 0

function mod( num : integer, den : integer) : integer
  if num < den then
     num
  else
     mod(num-den, den)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I implemented this program in python

import math
def main(x):
return circularPrimesTo(x)

def circularPrimesTo(x):
return circularPrimesToHelper(x+1,2,0)

def circularPrimesToHelper(top,x,count):
if x<top:
if isCircularPrime(x):
return circularPrimesToHelper(top,x+1,count+1)
else:
return circularPrimesToHelper(top,x+1,count)
else:
return count

def isCircularPrime(x):
if isCircularPrimeHelper(x,math.log10(x)+1):
return report(x)
else:
return False

def isCircularPrimeHelper(x,turns):
if turns==0:
return True
else:
return isPrime(x) and isCircularPrimeHelper(rotate(x),turns-1)

def report(x):
print(x)
return True

def rotate(x):
return x/10+((x%10)*(10**math.log10(x)))

def isPrime(n):
return not hasDivisorFrom(2,n)

def hasDivisorFrom(i,n):
if i<n:
return divides(i,n) or hasDivisorFrom(i+1,n)
else:
return False

def divides(a,b):
return (b%a==0)


print(main(37))
  

But I got RecursionError: maximum recursion depth exceeded in comparison

Add a comment
Know the answer?
Add Answer to:
convert this program to python to see it this program is valid or not (* ported...
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
  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • write easiest program to under stand:- a) Python program that takes three coefficients (a, b, and...

    write easiest program to under stand:- a) Python program that takes three coefficients (a, b, and c) of a Quadratic equation (ax2+bx+c=0) as input and compute all possible roots b).User defined function isprime(num) that accepts an integer argument and returns 1 if the argument is prime, a 0 otherwise. Write a Python program that invokes this function to generate prime numbers between the given ranges

  • CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream>...

    CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream> using namespace std; int main() { //declaring variable num int num; //prompting user to enter a number cout<<"Input a number to check prime or not:"; //reading input from user cin>>num; //loop to check user input for positive number while(num<0) { cout<<"Error! Positive Integers Only.\n"; cout<<"Input a number to check prime or not:"; cin>>num; } //initializing isPrime variable with false bool isPrime = true; //loop...

  • In ASCII C programming write a function that determines if an integer is prime. A prime...

    In ASCII C programming write a function that determines if an integer is prime. A prime number is one that is not divisible by any number other than one and itself. int isPrime(long num); Return 1 if num is prime, 0 if not. As above, make this a pure function contained in its own file isPrime.c and test it with a separate tester program. To test if an integer is prime you could implement a loop that generates trial divisors...

  • Prime Number Programing in C Note: The program is to be written using the bitwise operation....

    Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...

  • Write a C++ program that computes the following series: sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime Your program...

    Write a C++ program that computes the following series: sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime Your program should prompt the user to enter a number n. The program will compute and display sum based on the series defined above. firstPrime: is 2 secondPrime: the first prime number after 2 thirdPrime: the third prime number …. nth prime: the nth prime number Your program must be organized as follows: int main() { //prompt the user to enter n //read n from the...

  • A prime number is a positive integer whose positive integer divisors are 1 and the number....

    A prime number is a positive integer whose positive integer divisors are 1 and the number. E.g. 17 is a prime number -23 is a not prime number 28 is not a prime number -45 is not a prime number The only even prime number is 2 Write a boolean function, isPrime that return true if its integer parameter is a prime and false if its integer parameter is not a prime number. A simple prime number algorithm is if...

  • Assignment Develop a program to analyze one or more numbers entered by a user. The user...

    Assignment Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. Input should be limited to numbers from 1 through 1000. Determine if a number is a prime number or not. A prime number is one whose only exact divisors are 1 and the number itself (such as 2, 3, 5, 7, etc.). For non-prime numbers, output a list of all divisors of the number. Format your...

  • Create a program named Lab14B. You will create a nested loop in this program by putting...

    Create a program named Lab14B. You will create a nested loop in this program by putting Lab14A’s for loop inside a while loop. In the new while loop, you will read integers from the file, Lab14B.txt, and determine if the number is prime (using the copied loop from Lab14A). Stop the while loop when you reach the end of file. Copy your code from Lab14A into your new program Add a while not eof loop around that code, and also...

  • a prime number is a number that is only evenly divisible by itself and 1. for...

    a prime number is a number that is only evenly divisible by itself and 1. for example the number 5 is prime because it can only be evenly divided by 1 and 5 the number 6 however is not prime because it can be divided evenly by 1,2,3 and 6. write a function name isPrime which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. use this functuion in a...

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