Solved

dividesSelf challange

Posted on 2016-08-30
15
113 Views
Last Modified: 2016-09-03
Hi,

I am working on below challeng



I have not undersood the description.



We'll say that a positive int divides itself if every digit in the number divides into the number evenly. So for example 128 divides itself since 1, 2, and 8 all divide into 128 evenly. We'll say that 0 does not divide into anything evenly, so no number with a 0 digit divides itself. Note: use % to get the rightmost digit, and / to discard the rightmost digit.

dividesSelf(128) → true
dividesSelf(12) → true
dividesSelf(120) → false

why if 0 then doe not devide and false?

please advise
0
Comment
Question by:gudii9
15 Comments
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 125 total points
ID: 41777222
The challenge is declaring that a zero digit results in false, so you don't really need to know why. I'm betting the reason is so you don't have to worry about divide-by-zero errors, though.
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41777326
Do you have a calculator?
Try dividing 120 by 1, 2, and then 0.
When that doesn't work, try dividing by some number close to zero, like 0.001
0
 
LVL 7

Author Comment

by:gudii9
ID: 41777427
128 divides itself since 1, 2, and 8 all divide into 128 evenly.

what it means by 1, 2 and 8 devides evenly for 128?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41777428
128
are they saying 128 consist of 3 digits which are 1 then 2 then 3??
0
 
LVL 27

Assisted Solution

by:d-glitch
d-glitch earned 125 total points
ID: 41777439
128 consist of 3 digits which are 1 then 2 then  8.

You can use % to determine if a division is even.

You can't hope to solve this until you understand the has One challenge.
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41777443
what it means by 1, 2 and 8 devides evenly for 128?

Divide evenly means that the remainder is zero.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41777445
Divide evenly means that the remainder is zero with  1 separately


Divide evenly means that the remainder is zero with 2 separately

Divide evenly means that the remainder is zero with 8 separately

or
Divide evenly means that the remainder is zero with 1, 2, 3 combinedly i.e 1*2*8
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 27

Expert Comment

by:d-glitch
ID: 41777449
Separately.
0
 
LVL 3

Accepted Solution

by:
Donna earned 250 total points
ID: 41777453
128 / 1 = 128 remainder 0 (remainder zero, means "evenly"
128 / 2 = 64 r-0 (divides evenly)
128 / 8 = 16 remainder 0, so 128 divides by 1, 2, and 8 evenly, with  "no remainder" ...so for the value 128 the function needs to evaluate to TRUE.

65 evaluates to false because 65/6 = 10.833333 , which is not "evenly"

and zero is ALWAYS false (any value with a zero anywhere) because anytime you divide by zero, it's an irrational number.

does that help at all?

so the criteria of the problem is this:
#1 determine the number of digits
#2 you need to divide the number by each of it's digits and if the remainder is zero, that part evaluates to TRUE. (and you can use the "modulus" operator...In Java, the % means modulus...and all it does is determine the remainder. So for example, 65%6=10.833333 , and should return FALSE. 65%5 = 0 and should return TRUE.  Modulus says "divide the 1st number by the second number and return the remainder of the operation that "does not" go evenly. So given any number x and y,
if x % y = 0{
   return TRUE;
}
else
   return FALSE;   //BUT you have to do that for each digit in the number.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41783167
public boolean dividesSelf(int n) {
  
  for (; n>0;) {
			if (n % n == 0)
				return true;
			n = n / 10;

			
		}
		return false;
}

Open in new window


i am failing some tests
Expected      Run            
dividesSelf(128) → true      true      OK      
dividesSelf(12) → true      true      OK      
dividesSelf(120) → false      true      X      
dividesSelf(122) → true      true      OK      
dividesSelf(13) → false      true      X      
dividesSelf(32) → false      true      X      
dividesSelf(22) → true      true      OK      
dividesSelf(42) → false      true      X      
dividesSelf(212) → true      true      OK      
dividesSelf(213) → false      true      X      
dividesSelf(162) → true      true      OK      
other tests
X      
Correct for more than half the tests
please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 41783169
Here is the URL of the challenge

http://codingbat.com/prob/p165941
0
 
LVL 7

Author Comment

by:gudii9
ID: 41783175
pseudo code is:
1. loop the given number.
2. if the reminder by dividing by 10 is 0 then return false
3. else if divide by 10 to go to each digit and if all those digits evenly divide by giving reminder of 0 then return true
4, return false
0
 
LVL 7

Author Comment

by:gudii9
ID: 41783192
package com.solution;

public class DevideItself {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("valiue is-->"+dividesSelf(213));

	}

	public static boolean dividesSelf(int n) {
		int k = 1;
		boolean temp=false;
		while (n > 0) {
			if (n % 10 == 0)
				return false;
			else {
				k = (n % 10);

				if (n % k != 0)
					return false;
				else if(n%k==0&&!temp){
				  temp=true;
					//return true;
				}
				//n = n / 10;
				//return false;
				// k=n/10;
			}
		}
		return false;
	}

}

Open in new window


tried something like above going to infinite loop
public boolean dividesSelf(int n) {
		int k = 1;
		boolean temp=false;
		while (n > 0) {
			if (n % 10 == 0)
				return false;
			else {
				k = (n % 10);
				if (n % k != 0)
					return false;
			//	else if(n%k==0&&temp){
			else if(n%k==0){
				  //temp=true;
					return true;
				}
				//n = n / 10;
				//return false;
				// k=n/10;
			}
		}
		return false;
	}

Open in new window


above code fails 3 tests
Expected      Run            
dividesSelf(128) → true      true      OK      
dividesSelf(12) → true      true      OK      
dividesSelf(120) → false      false      OK      
dividesSelf(122) → true      true      OK      
dividesSelf(13) → false      false      OK      
dividesSelf(32) → false      true      X      
dividesSelf(22) → true      true      OK      
dividesSelf(42) → false      true      X      
dividesSelf(212) → true      true      OK      
dividesSelf(213) → false      true      X      
dividesSelf(162) → true      true      OK      
other tests
OK      

please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 41783209
public boolean dividesSelf(int n) {
		int k = n;
		if (n % 10 == 0)
			return false;
		// boolean temp=false;
		while (n > 0) {
			if (k % (n % 10) != 0) {
				return false;
			}
			n = n / 10;
		}

		return true;
	}

Open in new window


above passes all tests. Any improvements/alternate approaches?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41783212
something like recursion or string approach?
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 79
Windows Service to Receive TCP Packets 4 124
what is a "java.lang.System Property"   ? 20 57
replace a word with other 1 40
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now