+ 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

6th Apr 2019, 6:32 AM
Pranshu Ranjan
Pranshu Ranjan - avatar
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.
6th Apr 2019, 7:29 AM
Ipang
+ 12
Pranshu Ranjan Welcome! 😊 I saw you solve, 👍 I just suggested, if it suits you 😉 Okay! Have a nice coding friend! 🍻
6th Apr 2019, 3:52 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 11
Pranshu Ranjan Will help if you check whether a number can be expressed as sum of two prime numbers?
6th Apr 2019, 2:42 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 3
Pranshu Ranjan, Wow you're working on something great, I'll make sure to watch that video soon. Thanks for sharing 👍
6th Apr 2019, 2:23 PM
Ipang
+ 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; } }
6th Apr 2019, 9:12 AM
Pranshu Ranjan
Pranshu Ranjan - avatar
+ 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! 👍
6th Apr 2019, 2:12 PM
Ipang
+ 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!!
6th Apr 2019, 7:35 AM
Pranshu Ranjan
Pranshu Ranjan - avatar
+ 1
It's good you have a solution. Code on.
6th Apr 2019, 11:55 AM
Michael Williams
Michael Williams - avatar
+ 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
6th Apr 2019, 2:18 PM
Pranshu Ranjan
Pranshu Ranjan - avatar
+ 1
Danijel It's fine , I just wanted to get two distinct numbers and it's being solved . Thanks for your response!
6th Apr 2019, 2:58 PM
Pranshu Ranjan
Pranshu Ranjan - avatar
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.
6th Apr 2019, 11:22 AM
Michael Williams
Michael Williams - avatar
0
No need man! Everything is already done look above ,the code is written and is fine . Thanks for your response and idea!!
6th Apr 2019, 11:51 AM
Pranshu Ranjan
Pranshu Ranjan - avatar