Ranajoy Saha

Ranch Hand

Posts: 105

2

posted 5 months ago

Hi, there

I need help in understanding what is meant by this question. If someone can provide a few examples illustrating the problem, it would be really helpful for me. Here's the question.

Write a program to print three 3-digit numbers triad such that the second number is 2 times as the first number and the third number is 3 times as the first number.

Note that all 3 numbers should contain 1-9 all digits and there should not be any repetition of the digit.

Regards,

Ranajoy Saha

I need help in understanding what is meant by this question. If someone can provide a few examples illustrating the problem, it would be really helpful for me. Here's the question.

Write a program to print three 3-digit numbers triad such that the second number is 2 times as the first number and the third number is 3 times as the first number.

Note that all 3 numbers should contain 1-9 all digits and there should not be any repetition of the digit.

Regards,

Ranajoy Saha

Tim Moores

Saloon Keeper

Posts: 4035

94

posted 5 months ago

This is a triad of 3-digit numbers: 123 - 246 - 369

It fulfills the requirement that the 2nd number is twice as large the 1st, and the 3rd is thrice as large as the 1st. It does not fulfill the requirement that no digits are repeated.

Here's another one: 123 - 456 - 789

It fulfills the requirements that no digits are repeated, but not the one about the 2nd and 3rd numbers being twice and thrice the value of the 1st.

Your task is to generate triads that fulfill both requirements.

It fulfills the requirement that the 2nd number is twice as large the 1st, and the 3rd is thrice as large as the 1st. It does not fulfill the requirement that no digits are repeated.

Here's another one: 123 - 456 - 789

It fulfills the requirements that no digits are repeated, but not the one about the 2nd and 3rd numbers being twice and thrice the value of the 1st.

Your task is to generate triads that fulfill both requirements.

Campbell Ritchie

Marshal

Posts: 56584

172

posted 5 months ago

That is not a programming question so much as a numerology question. The hard part is working out the algorithm. If you do it programmatically, you will have to consider looping through all numbers from 100 to 333 and testing

I am sure you can work out how to enumerate the separate digits.

*i*,*i** 2, and*i** 3. That is the brute force approach, which is notoriously inefficient, but if you only have a few hundred options it will only take a few μs.I am sure you can work out how to enumerate the separate digits.

Benedict Wong

Greenhorn

Posts: 13

posted 5 months ago

Can I do it this way instead?

Rule 1: A triad of 3 digits.

Rule 2: 3rd number is 3 times of first number; 2nd number is twice that of first number.

Rule 3: Digits 1-9 must all be used without repetition.

First, I'll set the first digit of each number to 3, 6, 9 respectively, since they cannot go any higher in order to adhere to the Rule 1.

Then, for the first number, I'll try permutations of 3 with 1, 2, 4, 5, 7, 8. Multiply by 2 then see if Rule 3 holds. If yes, multiply by 3 and check Rule 3 again. If no, try next permutation.

The reason for this algorithm is because the quantity of numbers that I have to test will then reduce from a couple of hundreds to perhaps a couple of dozen.

Rule 1: A triad of 3 digits.

Rule 2: 3rd number is 3 times of first number; 2nd number is twice that of first number.

Rule 3: Digits 1-9 must all be used without repetition.

First, I'll set the first digit of each number to 3, 6, 9 respectively, since they cannot go any higher in order to adhere to the Rule 1.

`3xx`

6xx

9xx

6xx

9xx

Then, for the first number, I'll try permutations of 3 with 1, 2, 4, 5, 7, 8. Multiply by 2 then see if Rule 3 holds. If yes, multiply by 3 and check Rule 3 again. If no, try next permutation.

The reason for this algorithm is because the quantity of numbers that I have to test will then reduce from a couple of hundreds to perhaps a couple of dozen.

New to programming, but I'll catch up. OCA, here I come.

Campbell Ritchie

Marshal

Posts: 56584

172

posted 5 months ago

You can't assume that.

It could be:

Benedict Wong wrote:First, I'll set the first digit of each number to 3, 6, 9 respectively, since they cannot go any higher in order to adhere to the Rule 1.

3xx

6xx

9xx

You can't assume that.

It could be:

`1xx 2xx 4xx`,

`1xx 3xx 4xx`,

`1xx 3xx 5xx`,

`2xx 4xx 6xx`,

`2xx 4xx 7xx`,

`2xx 5xx 7xx`,

`2xx 5xx 8xx`or

`3xx 6xx 9xx`.

It is sorta covered in the JavaRanch Style Guide. |