![]() |
|
[Java] Modify this 3N +1 problem so that only certain ranges are accepted - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Modify this 3N +1 problem so that only certain ranges are accepted (/showthread.php?tid=5738268) |
Modify this 3N +1 problem so that only certain ranges are accepted - Clockism - Oct 16, 2017 So I have the 3N+1 problem already, I am just not sure where I need to insert, an if statement to adjust the ranges it accepts. https://gyazo.com/8970fee9207de0239d37f9cc3622e345 import java.util.Scanner; public class Lab3Problem1StewartSpencer { public static void main(String[] args) { /* Declare variables:*/ int start = -1; String seq; /* Create scanner: / Scanner stdin = new Scanner(System.in); / Get user input that is range-checked.*/ // Loop until the user gives a positive integer: do { // Prompt user for an integer: System.out.println("Please enter a even positive integer:"); // Get input: start = stdin.nextInt(); } while (start < 1); // Check that the input is positive and even. //} while (start < 1 || start % 2!= 0 ); // OLD: Check that the input is positive and even. // Begin the string report for out sequence: seq = start + ", "; /* Main work loop.*/ // Loop until the sequence reaches a value <= 1. while (start > 1) { if (start % 2 == 0){ // Is it even? start = start /2; } else { // Is sequence value odd? start = 3 start +1; } //System.out.println(start); // Create an updated report string: if (start == 1) { seq = seq + start + ", "; } else{ seq = seq + start + ", "; } } / Show results. */ System.out.println("Your sequence is:" + seq); } } * - Glass Bones - Oct 17, 2017 After receiving the input simply check the value: Code if((start >= 101 && start <= 1001) || (start >= 3003 && start <= 5005))You also need to only accept odd numbers when reading input, so make sure to include that as well. You can break it up into else if statements if it makes it easier for you to understand. |