Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A positive integer is magic if, and only if, it can be reduced to 1 by repeatedly dividing it by 2 if its even or

A positive integer is magic if, and only if, it can be reduced to 1 by repeatedly dividing it by 2 if its even or multiplying it by 3 and then adding 1 if its odd. So, for example, 3 is magic because 3 reduces first to 10 (3 3 + 1), then to 5 (10/2), then to 16 (5 3 + 1), then to 8 (16/2), then to 4 (8/2), then to 2 (4/2), and finally to 1 (2/2). The magic numbers hypothesis states that all positive integers are magic, or, formally: x Z, M AGIC(x) where M AGIC(x) is the predicate x is magic. Write a Python 3 or Java program to automate the search for a counter example to the magic numbers hypothesis for user definable ranges of positive integers. To code this, you should modify the given Python or Java file. Your function should accept two integers that will act as the lower and upper bounds respectively for the range of numbers that you should search. Upon completion, your function should return an integer as follows: If you find a counter example i in the specified range that is nota magic number, return 1 i (i.e., the negative of the non-magic number). Otherwise return the number of steps (where x/2 or 3x + 1 each count as one step) it took to reduce the number to 1. For example: the number 1 would generate the reduction sequence: 1, which is zero steps. the number 2 would generate the reduction sequence: 2 1, which is one step. the number 3 would generate the reduction sequence: 3 10 5 16 8 4 2 1, which is seven steps.

public class Assignment2_Java_Template { /* This method should accept the number to test and the maximum number of iterations * to try before halting execution. If num is NOT magic (or the maximum number * of iterations has been reached), return (-1 * num) (i.e., the negative of num). * If num IS magic, return the number of iterations it took to reduce num to 1. * * Remember that a number is magic if it can be reduced to 1 by dividing it by 2 if * it is even or multiplying it by 3 and adding 1 if it is odd. */ public static int IsMagic(int num, int max_iterations) { return 0; } /* This method should be used to check if each number in the range [start, stop] * is a magic number. If all numbers in the range are magic, return the number of * iterations that it took to reduce the number passed into "stop" to 1. If you * find a number that is NOT magic, this method should return the negative of * that number. */ public static int TestRange(int start, int stop, int max_iterations) { return 0; } public static void main(String[] args) { int start = 5; int stop = 20; int max_iterations = 500; int result = TestRange(start, stop, max_iterations); System.out.println(result); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

More Books

Students also viewed these Databases questions