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

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 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

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