I have a question where i want to find f(a) = f(b) using any pigeon hole principle. I have made the program and working fine by giving me the right results.

Here is my code:

public class array1 {

public static void main(String args[]) { final int TOTAL = 50; int x,y; int[] numbers = new int[TOTAL];

for( x = 0; x < numbers.length; x++) {

y =( ((x * x * x) + ( 3 * x)) % 50); // System.out.print("when x = " + x); numbers[x] = y; } int z = 0; for(int l = 0; l < numbers.length; l++) { z = numbers[l];

System.out.println("When x = " + l + " Then Y = " + z);

}

} }

THe code above gives me the following result: When x = 0 Then Y = 0 When x = 1 Then Y = 4 When x = 2 Then Y = 14 When x = 3 Then Y = 36 When x = 4 Then Y = 26 When x = 5 Then Y = 40 When x = 6 Then Y = 34 When x = 7 Then Y = 14 When x = 8 Then Y = 36 When x = 9 Then Y = 6 When x = 10 Then Y = 30 When x = 11 Then Y = 14 When x = 12 Then Y = 14 When x = 13 Then Y = 36 When x = 14 Then Y = 36 When x = 15 Then Y = 20 When x = 16 Then Y = 44 When x = 17 Then Y = 14 When x = 18 Then Y = 36 When x = 19 Then Y = 16 When x = 20 Then Y = 10 When x = 21 Then Y = 24 When x = 22 Then Y = 14 When x = 23 Then Y = 36 When x = 24 Then Y = 46 When x = 25 Then Y = 0 When x = 26 Then Y = 4 When x = 27 Then Y = 14 When x = 28 Then Y = 36 When x = 29 Then Y = 26 When x = 30 Then Y = 40 When x = 31 Then Y = 34 When x = 32 Then Y = 14 When x = 33 Then Y = 36 When x = 34 Then Y = 6 When x = 35 Then Y = 30 When x = 36 Then Y = 14 When x = 37 Then Y = 14 When x = 38 Then Y = 36 When x = 39 Then Y = 36 When x = 40 Then Y = 20 When x = 41 Then Y = 44 When x = 42 Then Y = 14 When x = 43 Then Y = 36 When x = 44 Then Y = 16 When x = 45 Then Y = 10 When x = 46 Then Y = 24 When x = 47 Then Y = 14 When x = 48 Then Y = 36 When x = 49 Then Y = 46

So the output is good. Now, I have to find when two Y have the same value.. so when x = 2 and Y = 14, and when x = 7, Y = 14. So in this way I have to find all the duplicate values of Y and print the their following x (index)

Guys I am almost done with my project. Could please tell me or write the code for me how to do it.

We try to focus here on helping people learn how to do things themselves, rather than just handing them the answer. Please don't just post the solution. In the long run, you're helping nobody.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11

posted

0

I still didn't understand.. what I trying to do is I want to count the all the duplicates Y corresponding to their X's (index value)

Originally posted by Mayur Soneta: Guys give me some hint...or code or ideas...

Are you familiar with associative arrays?

If you are not looking for anything space-wise efficient, could you not create an array of 50 elements -- one for each possible value of Y? Each element of this array a sequence of X values that yield Y (the element's index in this array).

How would you go about solving this problem? Think out aloud and the gurus here will point you in the right direction.

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery

I would use a Hashtable. If done properly, you'll have a string of x's for every y in the Hashtable. You, would then be interested in only the y's with multiple x values (example: x = 2 5 7) and not those with single values (x = 3). [ January 30, 2007: Message edited by: Dan Walin ]

Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11

posted

0

I would like to print

When x = 2 Then Y = 14 When x = 11 Then Y = 14 When x = 12 Then Y = 14 ........

So I would like to print all the same Values of Y with Their Index values of X. Please guys let me know I have to submit this my tomorrow.. I am almost done, but need some hintt how to do this..

Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11

posted

0

Guys I am online and working on my project,please help me out

Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11

posted

0

When x = 2 Then Y = 14 When x = 7 Then Y = 14 When x = 11 Then Y = 14 When x = 12 Then Y = 14 When x = 17 Then Y = 14 When x = 22 Then Y = 14 When x = 27 Then Y = 14 When x = 32 Then Y = 14 When x = 36 Then Y = 14 When x = 37 Then Y = 14 When x = 42 Then Y = 14 When x = 47 Then Y = 14 ========================== When x = 3 Then Y = 36 When x = 8 Then Y = 36 When x = 13 Then Y = 36 When x = 14 Then Y = 36 When x = 18 Then Y = 36 When x = 23 Then Y = 36 When x = 28 Then Y = 36 When x = 33 Then Y = 36 When x = 38 Then Y = 36 When x = 39 Then Y = 36 When x = 43 Then Y = 36 When x = 48 Then Y = 36

I want to get the results in this way, I'm getting now.. but only when I am hard coding.. below is my code:

public static void main(String args[]) { final int TOTAL = 50; int x,y; int[] numbers = new int[TOTAL];

for( x = 0; x < numbers.length; x++) {

y =( ((x * x * x) + ( 3 * x)) % 50); // System.out.print("when x = " + x); numbers[x] = y; } int z = 0; for(int l = 0; l < numbers.length; l++) { z = numbers[l]; if ( numbers[l] == 14) System.out.println("When x = " + l + " Then Y = " + numbers[l]); } System.out.println("=========================="); for(int l = 0; l < numbers.length; l++) { z = numbers[l]; if ( numbers[l] == 36) System.out.println("When x = " + l + " Then Y = " + numbers[l]); } } // End of main

}// End of Class

I dont want to hard code.. in the for loop as you can seee.

Guys give me some hint.. how do I print the results.. with comparing the numbers in array..

Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109

posted

0

Here's a rough sample that may get you to the solution. Each x, y pair is put into a Hashtable with "y" as the key. If there is already a y in the Hashtable, then the new x is appended to the existing value. When you're all finished creating the Hashtable, all the entries with more than one value for "x" would be what you want. At least this would be my approach. Run this test and see if it gives you a good idea of how you can use a Hashtable.

[ January 30, 2007: Message edited by: Dan Walin ]

Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11

posted

0

Thank you for sharing, but the code is not working.. I got many errors. in that. .. Wouldn't there would be any easist way to do this...

while I haven't looked at your code in detail, why not just do an if statement somewhere that is like:

Note: I'm new to java, so the syntax may be wrong, but I've had to do similar things in C++ and something like that worked...a bit crude, but it worked... [ January 30, 2007: Message edited by: Don Sartain ]

Don<br /> <br />"An unexamined life is not worth living." -- Socrates

Mayur Soneta
Greenhorn

Joined: Jan 04, 2006
Posts: 11

posted

0

Don. Thank you for your reply.. I already did this ... but the problem is I dont want to hard code as "14" ... If I am hard coding that I'm done with my problem.... I DONT WANT TO HARD CODE..

You could try a double iteration through like you have, but nest the iterations. Use a temporary variable to hold a value, compare it with the next loop, and if they match you're in business. Then to prevent duplicates you could set the value you are currently working with to something that would be impossible to be in your array, and make sure when you're testing equality to also check it against that particular impossible value.

Sorry if I'm not explaining myself well, but I hope it helps.

Nate

If it is just the solution you want there are sites like Rent A Coder that may be of more help. I think the whole spirit of the ranch is to foster learning through trial and a bit of coaching, or to answer specific pinpointed questions regarding the language and its constructs. I know I've been close to volunteering for Euthanasia a few times on some of the projects I was working on, but with a bit of coaching and experimenting myself I'd like to think I learned and actually absorbed valuable lessons in the process. [ January 30, 2007: Message edited by: Nathan Leniz ]

The very existence of flamethrowers proves that at some time, some where, some place, someone once said to themselves "I'd really like to set those people on fire over there, but I just can't get close enough".

Originally posted by Mayur Soneta: PLEASE GIVE THE SOLUTION....

We don't do that here. We will help you learn, we will answer questions, we will give you advice. But we don't give solutions to problems. A lot of times people are asking for homework solutions, which is generally a violation of a school's code of ethics. We don't wish to be involved in that.

Note that Dan didn't say the code would work, he said it was a hint to help you figure it out. Look at his code, see what he is doing, then work it into YOUR code. His example is not a complete program.