Sinisterly
[Challenge:1] Multiples of 3 and 5 - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Coding (https://sinister.li/Forum-Coding--71)
+--- Thread: [Challenge:1] Multiples of 3 and 5 (/Thread-Challenge-1-Multiples-of-3-and-5)

Pages: 1 2


[Challenge:1] Multiples of 3 and 5 - Vi-Sion - 11-25-2016

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.
Don't just answer this share the code that made you solve it.
If done you'll get yourself 50 points.
For extra 50 points do a function to reverse that sum and a function to sort it in ascending order.

Example:if the sum was 8673 reversing function will change it to 3768 and sorting it in ascending order will make it 3678

Happy coding guys !!


Java C++ C  Python and Ruby and GO can't be used anymore for this challenge, but there's plenty of languages you can work with



RE: Multiples of 3 and 5 - Pikami - 11-25-2016

I don't understand...
Is this a challenge? What 50 points? Where do I share the solution?


RE: Multiples of 3 and 5 - Vi-Sion - 11-25-2016

@Pikami you share the solution here in this thread .
I think you missed the thread with the rules and the explanation so here you go https://sinister.li/Thread-Competition-Rules-and-scoreboard.


RE: Multiples of 3 and 5 - DarkMuse - 11-25-2016

(11-25-2016, 08:23 PM)Vi-Sion Wrote: 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.
Don't just answer this share the code that made you solve it.
If done you'll get yourself 50 points.
For extra 50 points do a function to reverse that sum and a function to sort it in ascending order.

Example:if the sum was 8673 reversing function will change it to 3768 and sorting it in ascending order will make it 3678

Happy coding guys !!

Fucking easy mode, seems like an assignment though especially considering the time of school year. If I had to guess, the easiest way would be to create a recursive function with a two cases for 3 and 5 and the obligatory base case in which to end. To reverse, create a string variable to store the current number, modulo 10 to get the last number and divide by ten to remove the last digit, sorting would be easy considering it would be a matter of string sorting or using basic number processing which would require string to integer casting.

GG


RE: Multiples of 3 and 5 - Nyx - 11-25-2016

So you require 5000 points to win the competition but you're giving like 50 points per challenge and require each 'challenge' to be in a new programming language. At that rate I'm pretty sure the challenge is impossible for 99% of the programmer population.


RE: Multiples of 3 and 5 - Pikami - 11-25-2016

(11-25-2016, 09:01 PM)Vi-Sion Wrote: @Pikami you share the solution here in this thread .
I think you missed the thread with the rules and the explanation so here you go https://sinister.li/Thread-Competition-Rules-and-scoreboard.
Thanks, that explained a lot.

Here is my solution in C++ (Tested on windows):
Code:
#include <iostream> #include <vector> int getSum(); //A function that gets the sum of all the multiples of 3 or 5 below 1000 int reverseNum(int a); //A function that reverses a number int sortNum(int a); //A function for sorting a number int main(){ int sum = getSum(); std::cout<<"The sum of all the multiples of 3 or 5 below 1000: "<<sum<<std::endl; std::cout<<"This number reversed is: "<<reverseNum(sum)<<std::endl; std::cout<<"This number sorted number is: "<<sortNum(sum)<<std::endl; } int getSum(){ int sum = 0; for(int i=1;i<1000;i++) if(i%3==0 || i%5==0) sum+=i; return sum; } int reverseNum(int a){ int newNum = 0; //The new number while(a>0){ newNum*=10; newNum += a%10; a/=10; } return newNum; } int sortNum(int a){ int n = 0; //The number of digits std::vector<int> D; //The digits //Get the digits while(a>0){ D.push_back(a%10); a/=10; n++; } //Sort the vector for(int i=0;i<n;i++) for(int o=i;o<n;o++) if(D[o]<D[i]) std::swap(D[o],D[i]); //Build the new number int newNum = 0; for(int i=0;i<n;i++) newNum=newNum*10+D[i]; return newNum; }
I don't know if I was allowed to use built-in sorting algorithms so I've written my own.

Here's the output:
Code:
The sum of all the multiples of 3 or 5 below 1000: 233168 This number reversed is: 861332 This number sorted number is: 123368



RE: Multiples of 3 and 5 - Inori - 11-25-2016

Two- and one-lined. This challenge is essentially fizzbuzz by another name.
Spoiler: python
s=str(sum(n for n in range(1000) if not (n%3 or n%5)))
print("\n".join([s,s[::-1],''.join(sorted(s))]))

Spoiler: ruby
puts sum=1000.times.inject(0){|s,n|(n%3>0||n%5>0)?sConfused+n}.to_s,sum.reverse,sum.chars.sort.join



RE: Multiples of 3 and 5 - Nil - 11-25-2016

Java (Just wrote it all in main method and used built-in sorting for the ascending order, and yes I know a lot of this is unnecessary, ie, arrayLists for keeping multiples, but it helps me think initially by separating everything and being able to see the result of anything)
Spoiler:
Code:
import java.util.ArrayList; import java.util.Collections; public class MultipleFinder { public static void main(String[] args) { int multiple1 = 0; int multiple2 = 0; int multiple3 = 0; int multipleThreeTotal = 0; int multipleFiveTotal = 0; int multipleFifteenTotal = 0; int multipleTotal = 0; int reversedTotal = 0; ArrayList<Integer> MultiplesOfThree = new ArrayList<>(); ArrayList<Integer> MultiplesOfFive = new ArrayList<>(); ArrayList<Integer> MultiplesOfFifteen = new ArrayList<>(); while (multiple1 < 997) { multiple1 += 3; MultiplesOfThree.add(multiple1); } while (multiple2 < 995) { multiple2 += 5; MultiplesOfFive.add(multiple2); } while (multiple3 < 985) { multiple3 += 15; MultiplesOfFifteen.add(multiple3); } for (int multiple : MultiplesOfThree) { multipleThreeTotal += multiple; } for (int multiple : MultiplesOfFive) { multipleFiveTotal += multiple; } for (int multiple : MultiplesOfFifteen) { multipleFifteenTotal += multiple; } multipleTotal = multipleThreeTotal + multipleFiveTotal - multipleFifteenTotal; System.out.println("Total of the multiples of 3 and 5: " + multipleTotal); // reverse total ArrayList<Integer> sepDigits = new ArrayList<>(); int totalLength = String.valueOf(multipleTotal).length(); for (int n = 0; n < totalLength; n++) { reversedTotal = reversedTotal * 10 + multipleTotal % 10; sepDigits.add(multipleTotal % 10); multipleTotal = multipleTotal / 10; } System.out.println("Reversed Total: " + reversedTotal); // sort sum in ascending order Collections.sort(sepDigits); System.out.print("Total in ascending order: "); for (int number : sepDigits) { System.out.print(number); } } }


@Inori Doesn't give the correct answers.
@Tarew I believe he means each solution in the threads must be in a different language, not for each person for every challenge.
@Pikami nvm


RE: Multiples of 3 and 5 - Vi-Sion - 11-26-2016

@Tarew i think @GOD explaned it for you, thanks @GOD Smile
@Pikami well done you got it right +100.
@Inori pls next time don't do it with two languages just choose one and do it with it (to keep a chance for others).
well about your code the reverse and sorting are working but the sum of the multiples isn't correct so +50 for now fix it and you'll get another +50.
@GOD good job you got it right +100
@DarkMuse the purpose isn't making a hard competition,if it was hard new people to coding won't learn anything from it so making it a mix would be better for the community at least that's my point of view .


RE: Multiples of 3 and 5 - bitm0de - 11-27-2016

Here's my solution in C: http://ideone.com/QjaYqg
Code:
#ifdef _MSC_VER # pragma warning(disable: 4996) #endif #include <stdio.h> #include <stdlib.h> #include <string.h> #define UNUSED(x) (void)(x) #define MAX_N 1000 int sum_multiples(int limit) { int i, sum; if (limit == 0) return 0; sum = 0; for (i = 3; i < limit; i += 3) if (i % 15 != 0) sum += i; for (i = 5; i < limit; i += 5) sum +=i; return sum; } int reverse_digits(int n) { int x = 0; do { x = (x * 10) + n % 10; } while (n /= 10); return x; } int cmp_digits(const void *v1, const void *v2) { return *(int *)v1 - *(int *)v2; } int sort_digits(int n) { size_t i, cnt; int c, digits[10]; i = cnt = 0; do { digits[i++] = n % 10; } while (n /= 10); cnt = i; qsort(digits, cnt, sizeof(*digits), cmp_digits); c = 0; for (i = 0; i < cnt; ++i) c = (c * 10) + digits[i]; return c; } int main(int argc, const char *argv[]) { unsigned int sum = sum_multiples(MAX_N); printf("sum = %d\n", sum); printf("reversed = %d\n", reverse_digits(sum)); printf("sorted = %d\n", sort_digits(sum)); UNUSED(argc); UNUSED(argv); return 0; }

(11-25-2016, 10:08 PM)Pikami Wrote: Here is my solution in C++ (Tested on windows):

...

@Pikami - You could have just used std::map<int, int> and it would have already been sorted on insertions.

Ex:
Code:
#include <iostream> #include <map> int sum_multiples(int limit) { int i, sum = 0; if (limit == 0) return 0; for (i = 3; i < limit; i += 3) if (i % 15 != 0) sum += i; for (i = 5; i < limit; i += 5) sum +=i; return sum; } int main() { int n = sum_multiples(1000); std::map<int, int> digits; std::cout << "sum = " << n << '\n'; std::cout << "reversed = "; int d; do { d = n % 10; std::cout << d; ++digits[d]; } while (n /= 10); std::cout << '\n'; std::cout << "sorted = "; for (const auto &kv : digits) for (int i = 0; i < kv.second; ++i) std::cout << kv.first; }