+ 1
How to generate two distinct numbers whose sum is equal to given number
Hello guys ! I have a problem set in which I have to generate two distinct numbers whose sum is equal to the given number . Suppose 8 is a number ,then I have to generate any two distinct numbers whose sum will be equal to 8. Sample output. Numebr:8 Two numbers are: 6 2 One more thing is there ,both numbers should not contain the digit 4. Like it's should not be in this way---- Number :17 Two numbers are:13 4 Above output is wrong!! So anyone one can help me in getting the logic
12 Réponses
+ 2
Just an idea I have in mind:
let x = 1, y = number - 1
loop while y greater than or equal to 1
if x + y equal to number
if x not equal to 4 and y not equal to 4
// we found a pair
x++
y--
But this way we may have multiple possibilities, I'm not sure which to choose if it happens. BTW is 4 an exception for any number of is it just for number 8? and how about 14 3 for number 17? 14 also has digit 4, so is 14 3 a valid pair for number 17?
(Edit)
I'm not even sure whether you want both number being not equals to 4 or neither number contains digit 4.
+ 12
Pranshu Ranjan Welcome! 😊
I saw you solve, 👍
I just suggested, if it suits you 😉 Okay!
Have a nice coding friend! 🍻
+ 11
Pranshu Ranjan
Will help if you check whether a number can be expressed as sum of two prime numbers?
+ 3
Pranshu Ranjan,
Wow you're working on something great, I'll make sure to watch that video soon.
Thanks for sharing 👍
+ 2
thank you friend for helping me I am really glad!!
i made some changes in the code with your logic and it's fine.
here is the code:-------------------------
import java.util.*;
public class Foregone{
public static void main(String[] args){
int n,x,y;
n=21;
x=1;
y=n-1;
while(y>=1){
if(x+y==n){
if(!(hasFour(x))&&!(hasFour(y))){
System.out.println("found pairs are "+y+" "+x);
break;
}
}
x++;
y--;
}
}
static boolean hasFour(int num) {
int rem;
while (num > 0) {
rem = num % 10;
if (rem == 4)
return true;
num = num / 10;
}
return false;
}
}
+ 2
Pranshu Ranjan,
Good job my friend! you solved the problem. And now I understand clearly that you want neither x or y to have digit 4, great work! 👍
+ 1
Yep! 4 should not be in the digits we get for any sum ,there is no exception for a particular number .
For sum 17 : 14 3
Both digits is okay but 14 contains the digit 4 ,that's why this answer would be wrong.
And I think you're logic is somewhere right !! I will look forward to implement it .
Thank for your response!!
+ 1
It's good you have a solution.
Code on.
+ 1
Ipang .
Thanks ! Actually this question was asked in one of the challenge of conducted by Google.
Hey friend Take time ,and watch my recent project also!
Presenting you Sam a digital assistant .its same like Microsoft crotana ,amazon alexa or say Jarvis is here .
Do watch and let me hear what you think about it.
Link is below---
https://youtu.be/jDsmymypNdo
+ 1
Danijel
It's fine , I just wanted to get two distinct numbers and it's being solved .
Thanks for your response!
0
I won't give you the exact code, however try this approach.
Consider x is your target number.
nummax=x-1 nummin=1
Compare every value in between from nummin to nummax via stepping loop. Use logic to eliminate sum=x/2.
0
No need man! Everything is already done look above ,the code is written and is fine .
Thanks for your response and idea!!