Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Modify this 3N +1 problem so that only certain ranges are accepted

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);
}
}
[Image: UJB3B5t.png]
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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by zlg43647Oct 28, 2022 07:06 AM 2 241 Oct 28, 2022 07:06 AM
Last Post: zlg43647
[C#] by mayar2021Mar 14, 2021 03:44 PM 7 296 Mar 14, 2021 03:44 PM
Last Post: sniper coder
[.NET] by mayar2021Mar 14, 2021 10:21 AM 4 205 Mar 14, 2021 10:21 AM
Last Post: HFDone