If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
$sum = 0; for ($i = 1; $i < 1000; ++$i) { if (0 == $i % 3 || 0 == $i % 5) { $sum += $i; } } echo $sum;
var total = 1000; var sum = 0; for (var i = 0; i < total; i++) { sum += checkDivisibility(i); } console.log(sum); function checkDivisibility(num){ if (num%5==0 || num%3==0) { return num; } else { return num=0; } }
int count= 1; float sum = 0; while (count<1000) { if (count%5 ==0) { sum+=count; count+=1; } else if (count%3 ==0) { sum += count; count+=1; } else { count+=1; } } println(sum);
I've used it before - it's really good for creating things that involve geometry and graphics (both 2D and 3D). I've made a professional software with it for a client - and I also made a physics simulator with it for fun lol.processing — VagabondSpectre
Java* :Plibrary of functions for javascript)... — VagabondSpectre
I've used it before - it's really good for creating things that involve geometry and graphics. I've made a professional software with it for a client - and I also made a physics simulator with it for fun lol. — Agustino
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
int primePosition =6; float testPrime = 13; while (primePosition < 10001) { testPrime +=2; for (int i = 2; i < testPrime/2; i++) { if (testPrime%i == 0) { break; } if (i == floor(testPrime/2)) { primePosition +=1; break; } } } println(testPrime); println (primePosition);
No never used it before! But I would imagine it's more useful for things like designing the front-end of games/applications rather than just website development. Looks pretty much like a Processing version for the web :PYou mentioned web development, have you used P5.js ? — VagabondSpectre
That's cool! I'm not very familiar with it, as you can see, haven't used it before.It's like processing but new and with CSS/DOM/HTML functionality so you can more easily structure page design. I use brackets for P5.js... — VagabondSpectre
That's because you have two loops nested inside each other. That should preferably be avoided. I'm not a computer scientist so I don't understand the theory very precisely, but I do know that having loops nested increases run-time significantly.I would be very impressed if someone could get a solution in shorter code (although the run time is a bit high on this one)... — VagabondSpectre
That's because you have two loops nested inside each other. That should preferably be avoided. I'm not a computer scientist so I don't understand the theory very precisely, but I do know that having loops nested increases run-time significantly. — Agustino
The one I did was similar - I looked into Brownian motion.Right now I'm building a thermodynamics simulator as a learning project — VagabondSpectre
I will think about it, haven't solved that one yet. But I did say:Can you show me an example solution to that problem which does not use a nested loop? — VagabondSpectre
Of course there are some situations where this wouldn't be feasible.That should preferably be avoided. — Agustino
I will think about it, haven't solved that one yet. But I did say:
That should preferably be avoided. β Agustino
Of course there are some situations where this wouldn't be feasible. — Agustino
Are you familiar with Big-O notations? Two loops inside each other would be O(n2) as opposed to O(n) for a single loop if I'm not mistaken. Again I never studied computer science - I took courses on it - but never studied it properly (I have a degree in Civil Engineering), these are just some things I've picked up with regards to algorithms. Big-O is used to classify the time efficiency of an algorithm.I don't think there's anything inherently wrong with nested loops other than it carries the chance of iterating through index values which are redundant or need not be checked. If I stored each prime as I found it and used that data to rule out future testPrimes and indexes of the loop that checks it's factors, it could be made faster still. — VagabondSpectre
But if you think about it, it makes sense. For each value of loopOne, the whole of loopTwo needs to be completed. That's a lot more calculations than having, say, two independent loops — Agustino
int[] array1 = { 1, 2, 3, ..., n} int[] array2 = { 1, 2, 3, ..., n} for (int i=0; i <array1.length; i++) { for (int j = 0; j <array2.length; j++) { if (array1[i] == array2[j]) { playGongNoise(); } } }
int[] array1 = { 1, 2, 3, 4, ..., n} int[] array2 = { 1, 2, 3, 4, ..., n} for (int i=0; i <array1.length; i++) { if (array1[i] == array2[0]) { playGongNoise(); } if (array1[i] == array2[1]) { playGongNoise(); } if (array1[i] == array2[2]) { playGongNoise(); } if (array1[i] == array2[3]) { playGongNoise(); } ... ... if (array1[i] == array2[n-1]) { playGongNoise(); } }
The way you've written them that's true, there is no loss or gain. But there might be a better way to do what you're trying to do there (and that would be the way you avoid using nested loops). Arrays for example are just one data structure that you can use to hold your values. Depending what you want/need to do with the values, arrays may not be the best way to store the data... (for example - why do you want to compare every datapoint from one to every datapoint from the other? Are you looking for something? What's the goal in that comparison?) There's also binary trees, hashmaps, treemaps, stacks and more... You may just need a different type of data structure for your project. So you'll either use a library that already has the data structure you need, or you have to create it yourself.You need to perform the same number of checks either way, so there is no loss or gain either way except to keep it concise... — VagabondSpectre
The array processing paradigm of R (and other, older languages like APL) allows for compact coding of such problems. Here's an R coding that spits out the answer:If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
sum((x<-1:1000)[x %% 3 ==0 | x %% 5 == 0)
(findprime<-function(x,n) if (sum(x %% 1:x == 0)<3) if (n>1) findprime(x+1,n-1) else x else findprime(x+1,n) )(2,10001)
public int makeChocolate(int small, int big, int goal) { int remainder = goal%5; if (goal > big*5+small || big*5 < goal - small || remainder > small) { return -1; } if (big*5 < goal) { return goal-big*5; } return remainder; }
Technically one loop. Still 0(n squared), but implemented as a state machine instead of loops.Anyway. After you guys were talking about writing the 10001st prime code without nested loops, I just couldn't resist. This code is mortifying (I got bored/tired by the time I got it working) and completely impractical, but satisfies the condition of having 'just one loop'. :P — Efram
Get involved in philosophical discussions about knowledge, truth, language, consciousness, science, politics, religion, logic and mathematics, art, history, and lots more. No ads, no clutter, and very little agreement β just fascinating conversations.