Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

# Trying to do mathematical theory (COLLATZ) in java

11 replies to this topic

### #1 sidd0123

sidd0123

• Banned
• 81 posts
• OFFLINE
•
• Local time:03:52 AM

Posted 12 March 2010 - 05:19 PM

How do i add a counter to my program that will count the number of steps taken to reach one. For example, if the value is 1, then the return value for the
method is 0, because it is already 1 and no steps were executed. Similarly, if the input parameter is
2, then the return value is 1, because 2 is even so it will be divided by 2 and become 1 after 1 step.

I'll write out my code. I know that there is something wrong with it on the return part, but I am fixing it right now. I have never written a counter for my programs and so i am asking for help. Thanks

public static int collatz(int given) {

if (given == 1)
return given;
else if (given % 2 == 0)
collatz(given / 2);
else
collatz(3 * given + 1);

return given;
}

### #2 PropagandaPanda

PropagandaPanda

• Malware Response Team
• 10,433 posts
• OFFLINE
•
• Gender:Male
• Local time:03:52 AM

Posted 12 March 2010 - 08:29 PM

Hello sidd.

Is this is classic x/2 , 3x+1 question?

You have a solution, though it's not an inefficient one.

```collatz(given / 2);
collatz(3 * given + 1);```

You are doing the operation, but you are not returning the resulting value.

With Regards,
The Panda

typo

Edited by PropagandaPanda, 12 March 2010 - 08:30 PM.

### #3 sidd0123

sidd0123
• Topic Starter

• Banned
• 81 posts
• OFFLINE
•
• Local time:03:52 AM

Posted 12 March 2010 - 10:55 PM

i may have gotten to the problem before you here, Ill post my fixed code

package package12;

public class Collatz {

public static String getAuthor() {
String name;
name = "Sid Patel";
return name;
}

public static int collatz(int given) {

int count = 0;

while (1 < given) {

if (given % 2 == 0) {
given = given / 2;
}

else
given = 3 * given + 1;

count = count + 1;
}
return count;

}

public static String maxString() {

return null;

}

}

I have yet to do the public static String maxString() method, so please ignore that. It should work, but I have also ran into another snare that I hope you can help me with.

When i input the number 63728127 into the window that pops up, it returns an incorrect valuel. I know this because I used a collatz (collatz is a theory that says if you pick a number and if it is even divide by 2 if it is odd multiply the number by 3 and add 1. If you keep repeating the process, you theoretically should end up with a one always.) calculator.

my question is, since i am working with integers, could i have overflowed the memory. If so what could i do to change that?

### #4 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:01:52 AM

Posted 13 March 2010 - 07:40 AM

could i have overflowed the memory. If so what could i do to change that?

Two questions for you. First, what is the biggest value an int can hold? Second, what is the next bigger data size for numbers? Those are easily found by doing a tiny bit of research.

### #5 sidd0123

sidd0123
• Topic Starter

• Banned
• 81 posts
• OFFLINE
•
• Local time:03:52 AM

Posted 13 March 2010 - 02:14 PM

the biggest value an int can hold is 2^31-1. i believe the next one is long, but im not sure as i have always been working with int.
Ohh i see what happened.

Are you familiar with the collatz theorem? If i have a piece of code that does some arithmatic in a loop until it reaches the number 1, what method could i use to find the largest number it got in the steps it took to get 1.

n = 100
Step 1, result 100.
Step 2, result 50.
Step 3, result 25.
Step 4, result 76.
Step 5, result 38.
Step 6, result 19.
Step 7, result 58.
Step 8, result 29.
Step 9, result 88.
Step 10, result 44.
Step 11, result 22.
Step 12, result 11.
Step 13, result 34.
Step 14, result 17.
Step 15, result 52.
Step 16, result 26.
Step 17, result 13.
Step 18, result 40.
Step 19, result 20.
Step 20, result 10.
Step 21, result 5.
Step 22, result 16.
Step 23, result 8.
Step 24, result 4.
Step 25, result 2.

see, i would return 100 because it it the largest of all the numbers i went through, but i dont know what method i could use to find and returnt that number

Edited by sidd0123, 13 March 2010 - 02:45 PM.

### #6 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:01:52 AM

Posted 13 March 2010 - 05:40 PM

Have a variable called 'biggest' set = to zero. Each time through the loop, see of the new value you calculated is bigger than the value being held in 'biggest'. If it is, replace the value in biggest with the new value.

the biggest value an int can hold is 2^31-1

Yeah, but how big is that exactly? And for that matter, why is it that size?

### #7 sidd0123

sidd0123
• Topic Starter

• Banned
• 81 posts
• OFFLINE
•
• Local time:03:52 AM

Posted 13 March 2010 - 07:08 PM

that would be 2,147,483,647 or int is 4 bytes and 32 bits, which most of our computer are running. thanks for biggest idea it worked.

If i have a local variable how would i get it to another method using a global variable

just a summary

like this is how i have it set up

method:

local variable: int given = number you put in;
does all the calcs
}//close method

method:

System.out.println(i want the given here, but the variable is in another method)

Edited by sidd0123, 13 March 2010 - 08:26 PM.

### #8 Romeo29

Romeo29

Learning To Bleep

• Members
• 3,194 posts
• OFFLINE
•
• Gender:Not Telling
• Location:127.0.0.1
• Local time:02:52 AM

Posted 16 March 2010 - 08:33 AM

If you want to calculate steps with inputting numbers like 63728127 then better use long data type for holding the value.

### #9 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:01:52 AM

Posted 16 March 2010 - 09:50 AM

Could I see your actual code please?

If i have a local variable how would i get it to another method using a global variable

If you have a global variable, then you don't need a local variable. As a global it is available everywhere. That is what it means to be global. If you want to use a local variable, then you just pass the variable to the method where it needs to be used.

### #10 sidd0123

sidd0123
• Topic Starter

• Banned
• 81 posts
• OFFLINE
•
• Local time:03:52 AM

Posted 18 March 2010 - 01:24 PM

Here is my code: I am just following the format that my teacher asked for, so I'm not really sure why he made us do that.

[codebox]package package12;

//This is the package that holds my class and methods

public class Collatz {

// This is the class that holds my methods

static int n;
static int counter;
static int sequence;

public static String getAuthor() {
String name;
name = "Sid Patel";
return name;
}

// This will return my name

public static int collatz(int given) {
// This is my method that will calculate and return the number of
// sequences it takes to reach 1
int count = 0;
// This initializes the counter

n= given;

while (1 < given) {
// Initiates the loop based on the given parameter
if (given % 2 == 0) {
given = given / 2;
}
// This tests the number to see if it is even or odd
// If even, it will divide the number by 2 and move on
// if not it will go to the else part of the if-else statement
else
given = 3 * given + 1;
// This will multiply the given number by 3 and add 1

count = count + 1;
// Keeps track of the number of times it repeated the process
counter = count;

}
return count;
// This returns the number of times the process was repeated
}

public static String maxString() {

System.out.println(n);
//Number with the maximum sequence

System.out.println(counter);
//Maximum sequence length

System.out.println();

String maxString = String.format();

return null;

}

}[/codebox]

This is the main my teacher will use to test my code:

[codebox]package package12;

import javax.swing.JOptionPane;

public class CollatzMain {

/**
* @param args
*/
public static void main(String[] args) {

String inputString;
String promptString = "";

while (true) {

inputString = JOptionPane.showInputDialog(null, promptString + "\n\nEnter n: ");
if (inputString == null || inputString.equals(""))
break;
int n = Integer.parseInt(inputString);

promptString = String.format("The Collatz sequence for %,d is %,d", n, Collatz.collatz(n));

}

promptString = Collatz.maxString();
//JOptionPane.showMessageDialog(null, promptString);
System.out.println(promptString);

}

}

[/codebox]

Edited by sidd0123, 18 March 2010 - 01:27 PM.

### #11 sidd0123

sidd0123
• Topic Starter

• Banned
• 81 posts
• OFFLINE
•
• Local time:03:52 AM

Posted 23 March 2010 - 12:10 PM

i got it

### #12 sidd0123

sidd0123
• Topic Starter

• Banned
• 81 posts
• OFFLINE
•
• Local time:03:52 AM

Posted 27 March 2010 - 12:51 PM

I was searching around to find a way to put in commas at certain intervals in a string, but didn't quite make sense to me.

If I have this method:

public static String maxString() {
//This method will return an organized string containing the three requirements

String organize = String.format("Number with maximum sequence:%10d %n Maximum sequence length:%10d %n Sequences generated:%10d", n, counter, sequence);
//This takes the string, called organize, and groups together the three requirements

return organize;
// This simply returns the formatted string to the main which when prints it out
}

This returns the following:

Number with maximum sequence: 23456777
Maximum sequence length: 229
Sequences generated: 4

I would like it to show:

Number with maximum sequence: 23,456,777
Maximum sequence length: 229
Sequences generated: 4

With commas

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users