|
Post by Eric on Feb 1, 2008 22:40:12 GMT
Here's to the start of programming challenges. These will be short simple challenges in which you program the solution to whatever it may be that I describe. Any programming language is acceptable (preferably a more human readable one though), but you must say what it is when posting your solution. For each challenge you will gain points, at which point I will implement a ranking system. Each challenge will be worth a given number of points. For finishing the challenge first you will receive an additional 3 points. For each challenge, the solutions will be ranked based upon brevity and elegance. I realize that it will vary from language to language, and that will be accounted for. The person with the best solution will receive an additional 5 points. You do not have to specifically sign up for these challenges, simply posting a solution will be enough to allow you to join. If you complete older challenges you will still receive points, but you will not be in competition for the best solution. Index:And to the start: Programming Challenge Number 1:Create a function that takes in any positive whole number and factors it down into prime numbers. The function will take one and only one argument, that being the number to be factored itself. It will return an array of the prime numbers that the number consists. The function may call on other functions, but it is not necessary. Points for Completion: 5
|
|
|
Post by Wrighty on Feb 2, 2008 1:04:00 GMT
Just change where it says: var num = and also, it outputs things in the array called factors. I have made the code write out the factors seperated by a comma, just for fun! <script type="text/javascript"> <!-- /*SSD Challenges #1 Created By Wrighty*/
var num = 10; //This is the number that you want prime factors of!
var factors = new Array(); for(i=0;i<=num;i++){ var num_fact = 0; if(num%i == 0){ for(a=2;a<i-1;a++){ if((i%1 == 0) && (i%i == 0) && !(i%a)){ num_fact++; } } if(num_fact == 0){ factors.push(i); }
} } document.write(factors.join(', ')); // --> </script>
|
|
|
Post by Me on Feb 2, 2008 3:16:37 GMT
I don't know if it's exactly elegant .... but, I thought I would do this to at least get me doing a little programming . I'm using Java: public class factors { public static void main(String[] args){ // use it here } public static int[] primefactors(int a){ String m = (a % 2 == 0) ? "2," : ""; for(int i = 3; i <= a; i+=2){ boolean b = (a % i == 0) ? true : false; for(int j = 2; j < i && b != false; j++){ b = (i % j == 0) ? false : b; } m += (b == true) ? (i == a) ? i : i + "," : ""; } /* java is really annoying for array size * it's not really possible to calculate a size for the int[] array at the start of the program * because we don't have know how many prime factors there is * so this next part is necessary ... */ String[] q = m.split(","); int[] s = new int[q.length]; for(int i = q.length-1; i > -1; i--){ s = Integer.parseInt(q); } return s; } }
|
|
|
Post by Eric on Feb 2, 2008 3:42:30 GMT
I don't know if it's exactly elegant .... but, I thought I would do this to at least get me doing a little programming . I'm using Java: public class factors { public static void main(String[] args){ // use it here } public static int[] primefactors(int a){ String m = (a % 2 == 0) ? "2," : ""; for(int i = 3; i <= a; i+=2){ boolean b = (a % i == 0) ? true : false; for(int j = 2; j < i && b != false; j++){ b = (i % j == 0) ? false : b; } m += (b == true) ? (i == a) ? i : i + "," : ""; } /* java is really annoying for array size * it's not really possible to calculate a size for the int[] array at the start of the program * because we don't have know how many prime factors there is * so this next part is necessary ... */ String[] q = m.split(","); int[] s = new int[q.length]; for(int i = q.length-1; i > -1; i--){ s = Integer.parseInt(q); } return s; } }To be honest I would have gone for an ArrayList, then use its toArray method, but yours is an interesting solution. Thanks for participating guys. Most of these challenges will be short and sweet like this, not anything too difficult, but something that is thought about. Just wondering, how often should I create a new challenge? Weekly? More often? Less?
|
|
|
Post by gray929 on Feb 2, 2008 4:58:43 GMT
2 a week since they are short. Great job with this Eric.
|
|
|
Post by Eric on Feb 2, 2008 5:12:53 GMT
LISP:(defun primalize (num) (loop for i from 2 to (sqrt num) do (if (= (mod num i) 0) (return-from primalize (append (list i) (primalize (/ num i)))))) (list num)) I'll post my javascript one later, because I figure that it might be a common language used.
|
|
|
Post by Wrighty on Feb 2, 2008 11:58:29 GMT
Mine was JS! And yea... more often, cause that one took about 10 mins if that... (including testing) soo... 3 a week?
|
|
|
Post by Eric on Feb 2, 2008 20:48:55 GMT
Well here's my thinking. I want to give time for people to a) see it each time there is one, b) code it, c) reflect upon it and see if they can simplify it any.
Also, this one was on a friday, meaning you'd have the weekend. However, if I do it more often it'll be the middle of the week, is that fine for everyone? This doesn't really affect me at all, its just what you guys feel capable of doing.
Oh yeah, even if you turn one in late you can still get points, but the judging of the best one will be done in time to post in the second challenge.
|
|
|
Post by Me on Feb 2, 2008 21:20:08 GMT
Sounds good. I definitely wouldn't want more then 2 a week. This is a good idea by the way . It might even get me back into coding ... that is - if I can get any spare time.
|
|
|
Post by Wrighty on Feb 2, 2008 22:13:25 GMT
ok 2 a week. But can we get another one tomorrow just because... well, #1 was on a friday?
|
|
|
Post by Eric on Feb 2, 2008 22:21:25 GMT
If I do one tomorrow, the next one will be on Thursday, then again on Sunday, following that pattern. Otherwise I'll wait until Monday and continue doing Monday and Friday ones.
|
|
|
Post by Wrighty on Feb 3, 2008 0:30:55 GMT
Thursday & Sunday sounds fine!
|
|
|
Post by Eric on Feb 3, 2008 16:29:35 GMT
Javascript:
function primalize(num) { for(i = 2; i <= Math.sqrt(num); i++) { if(num % i == 0) { return ().concat(primalize(num/i)); } } return ([num]); }
|
|
|
Post by Me on Feb 3, 2008 16:44:58 GMT
Javascript:function primalize(num) { for(i = 2; i <= Math.sqrt(num); i++) { if(num % i == 0) { return ().concat(primalize(num/i)); } } return ([num]); } Nice . As if I didn't think of the whole i <= Math.sqrt(num) thing. So, you're using recursion? I was going to try that, but I couldn't wrap my head around it. How does that check if it's prime?
|
|
|
Post by Eric on Feb 3, 2008 16:53:26 GMT
Nice . As if I didn't think of the whole i <= Math.sqrt(num) thing. So, you're using recursion? I was going to try that, but I couldn't wrap my head around it. How does that check if it's prime? Well mine, unlike both of yours', finds all the prime number so that if you were to multiply them out you'd get the original, which is why I don't have to check to see if a number is prime. Since the program starts at two every time, it eliminates all the lowest numbers first, so the later ones have to be prime. Let me give an example: primalize(1680) 2 * primalize(840) 2 * 2 * primalize(420) 2 * 2 * 2 * primalize(210) 2 * 2 * 2 * 2 * primalize(105) 2 * 2 * 2 * 2 * 3 * primalize(35) 2 * 2 * 2 * 2 * 3 * 5 * primalize(7( 2 * 2 * 2 * 2 * 3 * 5 * 7
|
|
|
Post by Me on Feb 3, 2008 17:16:01 GMT
Ah. Mind boggling, but it makes sense .
|
|
|
Post by derfleurer on Feb 25, 2008 20:05:24 GMT
Just for the hell of something to do...
JavaScript:
for(var i = 22, x = 2, n = []; x <= i; x ++) if(i % x == 0) { i = i / x; n.push(x --); }
|
|
Tobias
New Member
CSS Master
Rave like there's no tomorrow..
Posts: 39
|
Post by Tobias on Mar 3, 2008 21:13:20 GMT
Python: def primes(number): primes = [2,3,5] start = 7 while primes[len(primes)-1] < number: while not start % 3 or not start % 5: start = start+2 for a in xrange(len(primes)-2): if not start % primes[a+2]: break elif start**.5 < primes[a+2]: primes.append(start) break start+=2 return primes[:-1] def factor(number): numbers = [] onumber = number i = 0 while number > 1 : for i in primes(int(number**.5)): if not(number % i): numbers.append(i) number = number/i return numbers
Usage: factor(num) Returns array of all prime factors.
|
|