Multiples of 3 and 5 Vi-Sion Member   Posts: 91 Threads: 26 Joined: Jun 2015 Reputation: 4 06-16-2015, 06:52 PM 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);     } }``` NO-OP #LinuxMasterRace   Posts: 221 Threads: 32 Joined: Jun 2015 Reputation: 15 06-17-2015, 12:10 PM 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) Vi-Sion Member   Posts: 91 Threads: 26 Joined: Jun 2015 Reputation: 4 06-17-2015, 12:24 PM (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 Vi-Sion Member   Posts: 91 Threads: 26 Joined: Jun 2015 Reputation: 4 06-18-2015, 01:32 PM 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 NO-OP #LinuxMasterRace   Posts: 221 Threads: 32 Joined: Jun 2015 Reputation: 15 06-18-2015, 08:48 PM (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? Vi-Sion Member   Posts: 91 Threads: 26 Joined: Jun 2015 Reputation: 4 06-19-2015, 11:47 AM (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. bitm0de Junior Member  Posts: 34 Threads: 3 Joined: Sep 2015 Reputation: 4 09-06-2015, 05:20 AM (This post was last modified: 09-06-2015, 05:23 AM by bitm0de.) You could do something like this too: Code:```#include #include #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); }``` Vi-Sion Member   Posts: 91 Threads: 26 Joined: Jun 2015 Reputation: 4 09-06-2015, 11:49 AM ur code here isn't a c++ code ? cz thats how it looks to me bitm0de Junior Member  Posts: 34 Threads: 3 Joined: Sep 2015 Reputation: 4 09-06-2015, 05:20 PM My code is C, but you can still easily treat that structure as pseudocode and compile a Java equivalent.  Psycho_Coder Junior Member  Posts: 45 Threads: 15 Joined: Oct 2015 Reputation: 4 10-21-2015, 08:54 PM 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 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))` « Next Oldest | Next Newest » 