Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C# beginner

Name: Frost 2007-01-16 2:28

OK, first semester in C# and just starting to work on it.  Haven't programmed in a long time, kinda stumped on this one.  The assignment is to determine if a number is prime, and if its not, to factor it.  I know what a prime number is in my head, but I don't really know how to put that into code.

Thanks.

Name: Anonymous 2007-01-16 4:33

sub is_prime{map{$n*=$n%$_>0}2..($n=pop)/2;$n>1}

Name: Anonymous 2007-01-16 4:35

>>1
That's what your assignment is about, to know how to write that in code. I'll lend you a hand, which is much better for you than if I write it.

To know if a number is prime, you could try dividing it by anything - anything it should be divided by, that is. You wouldn't divide by 0 unless you're in /sci, you won't divide by 1 because it'll always work, so you want to start with 2. And where will you end? To check for primality, you'd end in the square root of the number. For example, for 100, you'd try 2 to 10. Of course, there are divisors higher than 10, like 20, but what's 100/20? 5 - you'd catch this one earlier on anyways.

So you should take the number, and loop from 2 to the square root of that number, trying to divide the number and seeing if it can be divided. How? When you can divide A by B (integer division), A mod B (A % B) is 0.

Then onto factoring. Think of how you do it by hand.

Name: Anonymous 2007-01-16 8:25


#!/usr/bin/perl -l

sub is_prime{my$n;map{$n*=$n%$_>0}2..($n=pop)**.5;$n>1}
sub factor{my$n,@f;map{until($n%$_){push@f,$_;$n/=$_}}2..($n=pop)/2;@f}

my$n=pop;
print is_prime($n)?"$n is prime.":"factors of $n: ".join',',factor$n

Name: Anonymous 2007-01-16 9:00

>>4

sub is_prime{$_='x'x pop;/../^/^(..+)\1+$/}
sub factor{my$n=pop;map{($n/=$_,push@_,$_)until$n%$_}2..$n;@_}

Name: Anonymous 2007-01-16 14:14

(defun primep (number)
  (when (> number 1)
    (loop for fac from 2 to (isqrt number) never (zerop (mod number fac)))))

(defun next-prime (number)
  (loop for n from number when (primep n) return n))

(defmacro do-primes (var-and-range &rest body)
  (let ((var (first var-and-range))
        (start (second var-and-range))
        (end (third var-and-range)))
    `(do ((,var (next-prime ,start) (next-prime (1+ ,var))))
         ((> ,var ,end))
       ,@body)))

Name: Anonymous 2007-01-16 20:31

>>6
Thanks for refreshing my mind on why shouldn't I learn Lisp. Lisp is the Perl of functional programming.

Name: Anonymous 2007-01-16 21:14

>>1
pseudocode:
IS-PRIME(N)
    if N ends in a 3, 5 or 7
        yeah
    else
        nah
sorry it's kind of hand wavy, I'm a researcher lulz

Name: Anonymous 2007-01-16 21:32

>>8
Wrong.  11 is prime and your function fails to think so.

Name: Anonymous 2007-01-17 1:08

>>7
More like the Java, it's so damned verbose.

Name: Anonymous 2007-01-17 7:45

>>10
It's verbose, but I'm not that worried about it as I'm worried about being cryptic and made of punctuation.

I said it's the Perl of FP because Perl is //()/$(=)/"&"#&%#?"()%/ and LISP is (((((()))))))))())))))))()()))))))))))))()))))((((())))))))))))))))))))) which is just as ugly.

Name: Anonymous 2007-01-17 9:40

erm primality testing is a very hard question. there are no efficient algorithms for it atm.

Name: Anonymous 2007-01-17 11:56

>>12
Since we're handling ints, we can safely use Fermat's theorem and check for all Carmichael numbers in the int range.

Name: Anonymous 2007-01-17 13:24

>>12
Try using a lookup table for like the first 1000 primes, and calculate from there other primes that are bigger.

Name: Anonymous 2007-01-17 14:21

>>11
that's the stupidest "lisp" example I've ever seen, and it's not even Lisp at all...

Name: Anonymous 2007-01-17 17:50 (sage)

>>12-14
this is probably efficient enough for most purposes...
may require some changes to work in your shitty language...

#include <math.h>
int is_prime(int n){for(int i=2;i<=sqrt(n);++i)n=n%i?n:0;return n>1;}

Name: Anonymous 2007-01-17 21:29

>>16
No one said it wasn't except for >>12.

Name: Anonymous 2007-01-18 4:02

>>15
Boy, you sure are easy to troll, even when I was not trolling. $(/()()="%/)="$&#"$/& is not Perl either (it may even be, but it's not intended to). I'm just describing how Perl  ( /(%)/="#$(/"=#()$/$ ) and LISP ( ((((())))))))()()()))()(((((()))))) ) programs feel.

Name: Anonymous 2009-01-14 14:18

Optimize THIS

Name: Anonymous 2009-03-06 6:13


The syntax highlighter gets   it horribly wrong.

Name: ​​​​​​​​​​ 2010-10-22 6:52

Name: Anonymous 2010-11-15 16:56

Name: Sgt.Kabukimanꘃ௦ 2012-05-28 21:49

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List