posted 8 months ago

- 1

It is seen that the number, 125874 and its double 251748, contain exactly the same digits, but in a different order. Write a java program to find and print the smallest positive integer, x such that 2x, 3x, 4x, 5x and 6x contain the same digits.

This is the question. I tried to do only for 2x numbers. But very hard to do it.

This is the code I tried. Get multiple, sum and length. But some numerics such as 225, 225*2=450

This also get the same multiplication, sum and number length. But they are not permuted.

I only want this with loops.

This is the question. I tried to do only for 2x numbers. But very hard to do it.

This is the code I tried. Get multiple, sum and length. But some numerics such as 225, 225*2=450

This also get the same multiplication, sum and number length. But they are not permuted.

I only want this with loops.

posted 8 months ago

Welcome to the Ranch!

It's always a good idea to Use Code Tags when posting code to make it easy to read. I added them for you this time.

It's always a good idea to Use Code Tags when posting code to make it easy to read. I added them for you this time.

Tim Driven Development

Kasun Nirmala

Greenhorn

Posts: 6

posted 8 months ago

I am very much new to this forum. Thank you for informing me.

Tim Cooke wrote:Welcome to the Ranch!

It's always a good idea to Use Code Tags when posting code to make it easy to read. I added them for you this time.

I am very much new to this forum. Thank you for informing me.

posted 8 months ago

Here is as possible approach to find out if two integers have permuted digits. It uses two helper static methods. The digits() method counts the number of occurrences of each digit in a given integer.

Then the permut() method decides if two integers have the same number of occurrences of each digit. This should do the job.

Then the permut() method decides if two integers have the same number of occurrences of each digit. This should do the job.

posted 8 months ago

Dominique Ubersfeld, please note this, which is written on the title page of this forum:-

We believe it doesn't help learning to provide a complete answer; indeed without explanation it may harm the learning process. Don't be annoyed with me, but I have pulled rank and removed the complete solution.We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

Campbell Ritchie

Marshal

Posts: 56610

172

posted 8 months ago

Kasun Nirmala, welcome to the Ranch again.

Please always tell us where such questions come from to avoid copyright problems and to give us a chance to assess the source for ourselves. We help you to learn, and don't supply complete solutions.

That looks like something out of Project Euler. Those exercises are to test your mathematical abilities more than your programming abilities. You would need to work out an algorithm for testing whether two numbers have the same digits in different order, e.g. 123 321. You would also need to work out how to count up. You should not at this stage say anything about loops; that is an implementation detail which only comes later in the process.

Experience will give you a nice simple rule: no algorithm, no code. Until you have thought what the algorithm is, you can't create the code. Once you have the algorithm, the code will be relatively simple to create.

Please always tell us where such questions come from to avoid copyright problems and to give us a chance to assess the source for ourselves. We help you to learn, and don't supply complete solutions.

That looks like something out of Project Euler. Those exercises are to test your mathematical abilities more than your programming abilities. You would need to work out an algorithm for testing whether two numbers have the same digits in different order, e.g. 123 321. You would also need to work out how to count up. You should not at this stage say anything about loops; that is an implementation detail which only comes later in the process.

Experience will give you a nice simple rule: no algorithm, no code. Until you have thought what the algorithm is, you can't create the code. Once you have the algorithm, the code will be relatively simple to create.

Kasun Nirmala

Greenhorn

Posts: 6

posted 8 months ago

Excuse me?

Dominique Ubersfeld wrote:Here is as possible approach to find out if two integers have permuted digits. It uses two helper static methods. The digits() method counts the number of occurrences of each digit in a given integer.

Then the permut() method decides if two integers have the same number of occurrences of each digit. This should do the job.

Excuse me?

Kasun Nirmala

Greenhorn

Posts: 6

posted 8 months ago

Actually I got it from my assignment. I have tried about 3 days now to overcome this problem. Tried many many ways. But invain. I couldn't overcome with any method. So, then I decided to ask from this forum.

Please help me on this. Can you please explain any algorithm to check the permute number?

Campbell Ritchie wrote:Kasun Nirmala, welcome to the Ranch again.

Please always tell us where such questions come from . . .

Actually I got it from my assignment. I have tried about 3 days now to overcome this problem. Tried many many ways. But invain. I couldn't overcome with any method. So, then I decided to ask from this forum.

Please help me on this. Can you please explain any algorithm to check the permute number?

Campbell Ritchie

Marshal

Posts: 56610

172

posted 8 months ago

Start by asking your instructor. If that is a basic programming assignment, I am starting to worry about whether it isn't unreasonably difficult. If Project Euler is about maths rather than programming then maybe that assignment is about maths rather than programming. Also ask your fellow‑students for advice, remembering there is a limit to how much you are allowed to tell one another. Had I not deleted a complete solution and had you used it, your plagiarism software would have found that and you would have got a mark of 0.

You will have to work with pencil and paper to create the algorithms. I suggest you start with a method for checking how many of each digit a number contains. You would do well to tell us some ideas. Even wrong ideas. Simply writing them down will help you think. Forget about the equality part for the time being. You can do that later.

Please don't quote the whole of a preceding post; that simply makes the thread longer and longer without adding any new information; I shall remove part of the quote.

You will have to work with pencil and paper to create the algorithms. I suggest you start with a method for checking how many of each digit a number contains. You would do well to tell us some ideas. Even wrong ideas. Simply writing them down will help you think. Forget about the equality part for the time being. You can do that later.

Please don't quote the whole of a preceding post; that simply makes the thread longer and longer without adding any new information; I shall remove part of the quote.

posted 8 months ago

First, I have to admit that I absolutely love solving mathematical algorithms. There is just something really elegant about a math solution versus a brute force solution.

... on the other hand, the pragmatic side of me says "how about getting to the solution first"? meaning do the brute force solution (for the comparison), finish the rest of the assignment; and then when you know that you have enough time, refactor the comparison algorithm.

Anyway, a possible brute force solution would be to break the number into an array of digits, reorder the digits from low to high (ie. sort it), and then, just iterate comparing one digit at a time.

Henry

Campbell Ritchie wrote:Those exercises are to test your mathematical abilities more than your programming abilities. You would need to work out an algorithm for testing whether two numbers have the same digits in different order, e.g. 123 321. You would also need to work out how to count up.

Kasun Nirmala wrote:

Actually I got it from my assignment. I have tried about 3 days now to overcome this problem. Tried many many ways. But invain. I couldn't overcome with any method.

Can you please explain any algorithm to check the permute number?

First, I have to admit that I absolutely love solving mathematical algorithms. There is just something really elegant about a math solution versus a brute force solution.

... on the other hand, the pragmatic side of me says "how about getting to the solution first"? meaning do the brute force solution (for the comparison), finish the rest of the assignment; and then when you know that you have enough time, refactor the comparison algorithm.

Anyway, a possible brute force solution would be to break the number into an array of digits, reorder the digits from low to high (ie. sort it), and then, just iterate comparing one digit at a time.

Henry

Campbell Ritchie

Marshal

Posts: 56610

172

posted 8 months ago

Anyway, Kasun Nirmala, that is a possible suggestion. I can see three stages to that process, which means you can implement it as three parts (or maybe even four). The more parts, the better.

Is that really brute force?Henry Wong wrote:. . . a possible brute force solution would be to break the number into an array of digits, reorder the digits from low to high (ie. sort it), and then, just iterate comparing one digit at a time.

Henry

Anyway, Kasun Nirmala, that is a possible suggestion. I can see three stages to that process, which means you can implement it as three parts (or maybe even four). The more parts, the better.

Kasun Nirmala

Greenhorn

Posts: 6

Kasun Nirmala

Greenhorn

Posts: 6

Campbell Ritchie

Marshal

Posts: 56610

172

It is sorta covered in the JavaRanch Style Guide. |