• Post Reply Bookmark Topic Watch Topic
  • New Topic

Modulus (%) Syntax  RSS feed

 
Harry Orson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As we know, 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
I was wondering if the MOD or % method can be used in a similar manner to the following:


Before this I was doing:

As you can see it's extremely inefficient for both the coder and the program to continously use "If(x % y == 0){" over and over (In other words I'm lazy, just how a coder should be XD)..
Any suggestions? Thanks.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harry Orson wrote:
As you can see it's extremely inefficient for both the coder and the program to continously use "If(x % y == 0){" over and over (In other words I'm lazy, just how a coder should be XD)..
Any suggestions?


First, how about coding it in a method? A method that takes a value X, and a list or array of values to check? They way, you only code it once, and use it many times.

Second, regarding the implementation of the method, how about using a loop? This way, you only have one condition, that is inside the body of the loop.

Henry
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Yes, good programmers tend to be lazy in a good way in that they don't want to work too hard to do something. Having a layer of if statements like that is too much work. You can do a for-loop with a guard clause. Now, this is where my good programmer laziness comes in. I gave you a hint on how to do it but I won't give you the code. If you're really a good programmer as you claim to be, then take my hint and go write the code yourself. 
 
Harry Orson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Welcome to the Ranch!

Yes, good programmers tend to be lazy in a good way in that they don't want to work too hard to do something. Having a layer of if statements like that is too much work. You can do a for-loop with a guard clause. Now, this is where my good programmer laziness comes in. I gave you a hint on how to do it but I won't give you the code. If you're really a good programmer as you claim to be, then take my hint and go write the code yourself. 

Thank you for the tips, although don't get me wrong, I love to joke but I'm no professional coder. Here's to improving... 
 
Harry Orson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
**[UPDATE]**
So I tried doing the things you guys mentioned...didn't go too well XD
This is my 'solution' for finding all values divisible by 1-20 below 10,000:

Obviuosly this is implemented incorrectly, any pointers?
 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should take another look at your initial if-condition approach, it was something like

Try to compare this to your new version. (Two keypoints: What was the resulting type? Did you modify x?)

Besides: You won't find any number below 10000 that is divisible by 1..20 as the lowest number is determined by the product of the different prime factors of the divisors.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!