Login Register






[Challenge:1] Multiples of 3 and 5 filter_list
Author
Message
RE: Multiples of 3 and 5 #6
(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-R...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
(This post was last modified: 11-25-2016, 10:12 PM by Pikami. Edit Reason: Added the output of my code )

[+] 1 user Likes Pikami's post
Reply





Messages In This Thread
[Challenge:1] Multiples of 3 and 5 - by Vi-Sion - 11-25-2016, 08:23 PM
RE: Multiples of 3 and 5 - by Pikami - 11-25-2016, 08:57 PM
RE: Multiples of 3 and 5 - by Vi-Sion - 11-25-2016, 09:01 PM
RE: Multiples of 3 and 5 - by Pikami - 11-25-2016, 10:08 PM
RE: Multiples of 3 and 5 - by DarkMuse - 11-25-2016, 09:58 PM
RE: Multiples of 3 and 5 - by Nyx - 11-25-2016, 10:06 PM
RE: Multiples of 3 and 5 - by Inori - 11-25-2016, 10:39 PM
RE: Multiples of 3 and 5 - by Nil - 11-25-2016, 10:51 PM
RE: Multiples of 3 and 5 - by Vi-Sion - 11-26-2016, 01:26 PM
RE: Multiples of 3 and 5 - by bitm0de - 11-27-2016, 12:46 AM
RE: Multiples of 3 and 5 - by Pikami - 11-27-2016, 10:17 AM
RE: Multiples of 3 and 5 - by Vi-Sion - 11-27-2016, 01:17 PM
RE: Multiples of 3 and 5 - by lux - 11-30-2016, 09:29 PM
RE: Multiples of 3 and 5 - by Vi-Sion - 11-30-2016, 10:11 PM