tags:

views:

329

answers:

7

I am trying to solve the following problem:

Problem:

Write a program that displays the integers between 1 and 100 that are divisible by either 6 or 7 but not both.

Here is my code:


import acm.program.*;

public class Problem4 extends ConsoleProgram
{
    public void run()
    {
        for (int i = 1; i <= 100; i++)
        {
            boolean num = ((i % 6 == 0) || (i % 7 == 0));

            if (num == true)
            println(i + " is divisible");
        }
    }
}


The above code shows the following answers: 6,7,12,14,18,21,24,28,30,35,36,42,48,49,54,56,60,63,66,70,72,77,78,84,90,91,96,98

Now the bold number " 42 and 84 " are both divisbile by 6 and 7. Now If i change the || to && in the above code, the result shows only 42 and 84.

What change should i do to remove these 2 numbers from the final result.

it seems to me as the answer is right in front of me but i am not able to get a hold of it.

Can someone help please ?

+1  A: 

Think about what it means to be divisible by 6 and 7... the answer to life the universe and everything.

Daniel A. White
"The Hitchhiker's Guide to the Galaxy" rocks)
Rorick
I agree :). Thank you.
kd304
+4  A: 

You need an extra check for "but not both". I think it should be:

boolean num = ((i % 6 == 0) || (i % 7 == 0)) && (i % 42 != 0);

duffymo
I already have that in my codeboolean num = ((i % 6 == 0) || (i % 7 == 0));
Ibn Saeed
Hit the enter too quickly. See edited result. Your problem isn't Java, it's logic.
duffymo
But your code is more of a hack rather than a solution as 42 and 84 are not known in advance.
Ibn Saeed
yes they are. "but not both" says it all. mine will catch 42, 84, and every other multiple of 6*7. That's what "but not both" means.
duffymo
I like this. 42 isn't a hack, as suggested above. It's the LCM of 6 and 7, so any number that's divisible by 42 will, of course, be divisible by _both_ 6 and 7. Nice.
mtnygard
Thanks, i tested the code and it worked. A very different way of solving this problem.
Ibn Saeed
-1 from me. I would not recommend that anybody use the Java language this way, even if it "works". Any decent language version should be able to handle the logical issue in a way that doesn't require this measure.
kd304
I'm sorry, I changed my mind about the -1 but I can't remove it as the system disallows the rewoke on older posts. I'll therefore make a fav+upvote on one of your questions.
kd304
+1  A: 
import acm.program.*;

public class Problem4 extends ConsoleProgram
{
    public void run()
    {
        for (int i = 1; i <= 100; i++)
        {
            boolean num = ((i % 6 == 0) || (i % 7 == 0));
            boolean both = ((i % 6 == 0) && (i % 7 == 0));

            if ((num == true) &&  (both == false))
            println(i + " is divisible");
        }
    }
}
Rorick
Thanks Rorick, it was right in front of me :)This solved the problem.
Ibn Saeed
You're welcome)
Rorick
+5  A: 

you have to make your condition look like:

boolean num = (i % 6 == 0 || i % 7 == 0) && !(i % 6 == 0 && i % 7 == 0);

that's basically converting "but not both" to Java code :)

cd1
Dang, i had this in my mind but did not try it out.
Ibn Saeed
You can almost translate it to words: (divisible-by-6 OR divisible-by-7) AND NOT (divisible-by-6 AND divisible-by-7)
Yuval A
Thanks Yuval, that helps as well.
Ibn Saeed
oxbow_lakes
the XOR version is more cryptic, although it works the same.
cd1
+16  A: 

XOR is the way to go.

import acm.program.*;

public class Problem4 extends ConsoleProgram {
  public void run() {
    for (int i = 1; i <= 100; i++) {
      if ( (i % 6 == 0) ^ (i % 7 == 0) ) {
        println(i + " is divisible");
      }
    }
  }
}
mtnygard
I think I will return my developer license.
kd304
Ibn Saeed
Xor is a binary operator. A ^ B means A or B but not both. != can be used as equivalent on booleans if ^ looks too alien to you, Ibn Saeed.
ggf31416
Took the code right out of my editor. Just one note though: It is bad style (in Java) to explicitly test a boolean expression. "if (i % 6 == 0 ^ i % 7 == 0) ..." is preferred.
William Brendel
I woud remove the == true
ggf31416
I followed the way of the author of the book. I used to do if ...
Ibn Saeed
@ggf31416, removed the (== true). Thanks for pointing that out for me.
Ibn Saeed
XOR is used seldom enough where most people wound have to look it up to remember it. That's why I'd prefer my way - more readable, and exactly the same thing logically. BTW, it's "Stanford".
duffymo
@myself - it's "would", not "wound". 8)
duffymo
lol @ duffymo ......
Ibn Saeed
@duffymo, I guess it depends on what kind of development work you do. I use XOR a lot in the lower level programming I do.
Simucal
-1. Sorry I had to do this to have your attention. I you can convince me you came to the same conclusion independently after 4 minutes of my own post, I will revoke my downvote. Don't worry, you lost only -2 rep and you still gained +120 btw. Have a nice day ^_^
kd304
@duffymo: This is why they don't use XOR gates to build chips, or use XOR for cryptographic purposes. Not to mention image manipulation through XOR. "Make everything as simple as possible, but not simpler." - Albert Einstein ;)
kd304
@kd304 - no worries. My notary public wasn't available when I posted the answer, but I will point out that mine is listed as "older" because I posted first. I had to edit because I erroneously hit the "return" key before adding that last clause. As I recall, by the time I typed in the comments pointing out my error and entering the "(i % 42 !- 0)" bit, your answer was up. I don't mind the criticism though, because I actually like the XOR answer better. No problem. Sincerely, %
duffymo
@kd304 - Can't go wrong with an Einstein quote. And your answer is superior to mine. Ups for you.
duffymo
@duffymo: See my last comment on your answer - I hope you recognize it from somewhere. There is always time to improve an answer (^_^)
kd304
@kd304 Simultaneity is a hard question in the web world. As I was typing my answer, I saw the banner up top about another answer to the question being posted. I didn't refresh the answer list because I didn't want to lose my work (even though it was just a few minutes.) These things happen, and priority isn't a crucial issue, so I'm not too concerned about it.
mtnygard
@mtnygard: Ok. I made a mistake. Forgot that the site does not allow the vote to be rewoked when the post itself was edited too long ago. I don't know how we could achieve 'simultaneity' on this matter but I really want to. I can't promise I'll keep on track with this post. But thank you, I'll repay you somehow.
kd304
@kd304 - No worries at all. I got what I wanted out of this - a fine conversation and learning experience. I'm happy to take instruction from you.
duffymo
+4  A: 

you can also try

boolean num = ((i % 6 == 0) != (i % 7 == 0));
Carlos Heuberger
For booleans, ^ does the same thing as != (see truth tables). After all, logical bicondition (xnor) is logical equality, and exclusive or is its negation. I happen to prefer !=, mainly because some languages I use don't have boolean xor, and != more-than-suffices. Many of the other examples include duplicate mod calculations, which is not terrible, but mod can slow tight code down :)
Gracenotes
A: 

Here is a snippet that should work as well- in C++ but change to boolean...

int value;
if ((value % 6 == 0 && value % 7 != 0) || (value % 6 != 0 && value % 7 == 0))
    cout << "Is " << value << " divisible by 6 or 7, but not both? true" << endl;
  else
    cout << "Is " << value << " divisible by 6 or 7, but not both? false" << endl;
iwanttoprogram