Multiples of 3 and 5
#1
Find the sum of all the multiples of 3 or 5 below 1000.

Code:
public class multiples {
    
    public static void main (String args[]) {
        int sum = 0;
for(int i=1; i<1000; i++)
{
    if (i % 3 == 0 || i % 5 == 0)
        sum += i;
}
System.out.println(sum);
    }
}
Reply
#2
How about this. I would like to see a function that given x amount of vars will check all of them and if they are a multiple of incremented int return true (printing out the number). Of course this is over a given range..

Example..
Code:
function solve(range, int1, int2, int3, .... int22) {
    // the fun stuff goes here
  }

Your functions should be able to take int arguments dynamically so solve(1000, 3, 5) should work as well as solve(999, 3, 7, 11, 15, 8)
Reply
#3
(06-17-2015, 12:10 PM)NO-OP Wrote: Your functions should be able to take int arguments dynamically so solve(1000, 3, 5) should work as well as solve(999, 3, 7, 11, 15, 8)

never thought this could be done thx for pointing this i'll take a look and see how to do it and share my code
Reply
#4
if i understood ur question right u give a range and how many variables u like and the function should get all the multiples of each variable in that range ?
if that's the case i'm done

Code:
public class multiples {

    public static void multiplesRange(int range, int... var) {
        for (int j = 0; j < var.length; j++) {
            System.out.println("the multiple of " + var[j] + " are :");
            System.out.println();
            for (int i = 1; i <= range; i++) {
                if (i % var[j] == 0) {
                    System.out.print(" " + i);
                }
            }
            System.out.println();
            System.out.println();
        }
    }

    public static void main(String args[]) {

        multiplesRange(1000, 3, 5, 11, 12);
    }
}

in other hand i'm no sure if you meant you need only the common multiples of the variables
Reply
#5
(06-18-2015, 01:32 PM)Luffy-_- Wrote: if i understood ur question right u give a range and how many variables u like and the function should get all the multiples of each variable in that range ?
if that's the case i'm done

Code:
public class multiples {

    public static void multiplesRange(int range, int... var) {
        for (int j = 0; j < var.length; j++) {
            System.out.println("the multiple of " + var[j] + " are :");
            System.out.println();
            for (int i = 1; i <= range; i++) {
                if (i % var[j] == 0) {
                    System.out.print(" " + i);
                }
            }
            System.out.println();
            System.out.println();
        }
    }

    public static void main(String args[]) {

        multiplesRange(1000, 3, 5, 11, 12);
    }
}

in other hand i'm no sure if you meant you need only the common multiples of the variables

Nope, this is exactly what I meant good job it's very interesting how java handles functions with extra option vars.

In java how many ints can you pass to this function before running out of memory?
Reply
#6
(06-18-2015, 08:48 PM)NO-OP Wrote: In java how many ints can you pass to this function before running out of memory?

i did read somewhere that the maximum safe number is Integer.MAX_VALUE - 8 Which is equal to 2147483639.
Attempts to allocate larger arrays may result in java.lang.OutOfMemoryError.
Reply
#7
You could do something like this too:
Code:
#include <stdio.h>
#include <stdlib.h>

#define B 0x39DB
#define MAX_N 1000

int main(void)
{
  int bits = B;
  int sum, v = sum = 0;
  while ((v += bits & 3) < MAX_N)
  {
    sum += v;
    if (!(bits >>= 2))
      bits = B;
  }
  printf("%d\n", sum);
  exit(0);
}
Reply
#8
ur code here isn't a c++ code ? cz thats how it looks to me
Reply
#9
My code is C, but you can still easily treat that structure as pseudocode and compile a Java equivalent. Smile
Reply
#10
I know this is the Java section but still I would share my code in Python

Code:
sum(i for i in range(1000) if i % 3 == 0 or i % 5 == 0)

But the above is a bruteforce way. What if I tell you I can save about 400 loop cycles ?

Think Logically and a bit mathematically as well.

How many multiple of 3 are present within 1000 --> int(999 / 3) = 333 i.e {3, 6, ... 15, ... 30, ... 999)
How many multiple of 5 are present within 1000 --> int(995 / 5) = 199 i.e. {5, 10, ... 15... 25, 30, ... 995}

Now subtract the number of common multiples that is 15. So, how many multiples of 15 are present ? ---> 66.

Now all of these are in A.P series. So what you can do is calculate the sum of these AP series and get the result.

The Formula for Sum of A.P. Series (3 variations are there but I will use a only one)

Sum_N_Terms = N * (A + L) / 2

where N = No of terms, A = First Term, L = Last term

Therefore the complete workout would be :-

{333 * (3 + 999) / 2} + {199 * (5 + 995) / 2} - {66  * (15 + 990) / 2} = 233168

Pretty Easy Smile

Now how is it an improvisation ?

The Bruteforce way would be to loop from 1 - 999 and check for multiplicity of 3 and 5 and then add. So the loop run for 999 times. But in the second case its just -> 333 + 199 + 66 = 598

So 401 loop cycles are saved.

Improvised Python Code

Code:
sum(i for i in range(3, 1000, 3)) + sum(i for i in range(5, 996, 5)) - sum(i for i in range(15, 991, 15))
Reply