java - Counting multiples in a range -
java - Counting multiples in a range -
so, using java trying figure out number of integers divisable 2, 3, , 5 in range [a,b]. wanted work big numbers, coded using inclusion exclusion principle. however, still have no thought how finish code, i.e. write code calculate multiples of x (2, 3, 5, 10, 15 , 30).
here's incomplete code:
class multiples { public static void main(string[] args) { long m = 0; scanner sc = new scanner(system.in); long = sc.nextlong(); long b = sc.nextlong(); ?????????? m = multiplesof2 + multiplesof3 + multiplesof5 - multiplesof6 - multiplesof10 - multiplesof15 + multiplesof30; system.out.println(m); } }
so, i'm missing '????' part. tried calculating multiples (in case of 2) expression:
multiplesof2c = math.ceil(a/2)*2; multiplesof2f = math.floor(a/2)*2; multiplesof2 = ((multiplesof2d - multiplesof2f)/2 + 1);
however, doesn't seem trick. result correct, in cases it's off 1 or 2. ideas, please?
simple not much when considering performance!
int counterx=0; int x=// desired number (long i=a; i<=b; i++) { if(i%x==0){ counterx++; } }
java math division
Comments
Post a Comment