Find prime factors

10

1

A whole program to find the prime factors of a given number. The number will be given on standard input.

cbrulak

Posted 2011-02-07T20:33:54.897

Reputation: 265

Question was closed 2014-01-19T08:13:20.363

1Should we make a function or a whole program? Any time constraints or limit to the input number? – Juan – 2011-02-07T20:43:10.380

updated description, thanks. – cbrulak – 2011-02-07T20:59:10.770

given number? from STDIN I presume? – Wile E. Coyote – 2011-02-07T21:00:02.017

@dogbert yes, from STDIN – cbrulak – 2011-02-07T21:10:32.430

1Output format? Space-separated, comma-separaed, each factor on a line? Sorted? With exponents? – Joey – 2011-02-08T00:35:28.600

1space is fine for output, sorted wasn't specified and I wasn't thinking exponents would be used. just full number. – cbrulak – 2011-02-08T03:29:04.717

Answers

6

Bash Shell

6 Chars

factor

If rot13 can be allowed, i don't see why this one is an issue...I'm sorry but this is very trivial.

st0le

Posted 2011-02-07T20:33:54.897

Reputation: 2 002

1You could argue that this one is more permissible than rot13, as this one is part of coreutils which is completely standard, and rot13 is part of... bsdgames? – Nabb – 2011-02-08T12:45:59.957

@Nabb: what? are you suggesting bsdgames is non-standard? I even have them on my DS (using DSLinux). – ninjalj – 2011-02-08T20:51:02.347

6

J - 2 characters

q:

Example:

   q: 31415
5 61 103

MPelletier

Posted 2011-02-07T20:33:54.897

Reputation: 300

5

dc, 48 chars

[ldp0<x]sp?2sd[[dsrld~0=p]dsxxcld1+sdlrd1<y]dsyx

ninjalj

Posted 2011-02-07T20:33:54.897

Reputation: 3 018

4

Ruby - 50 49 47 44 42 chars

2.upto(n=gets.to_i){|i|n/=p i while n%i<1}

Wile E. Coyote

Posted 2011-02-07T20:33:54.897

Reputation: 943

the () for n=gets.to_i are superfluous... – st0le – 2011-02-08T19:01:32.187

@st0le, ah thanks, made the change. – Wile E. Coyote – 2011-02-08T19:11:58.740

In general, map is 1 character shorter than each. ;) But in this case, you can just use 2.upto(n=gets.to_i) to save 2 characters. – Ventero – 2011-02-08T22:35:54.000

@Ventero, updated, thanks! – Wile E. Coyote – 2011-02-08T23:08:28.243

Crossed out 44 is still 44 – programmer5000 – 2017-05-02T11:39:11.153

3

Python: 58

n=input()
for i in range(2,n+1):
 while n%i<1:print i;n/=i

marcog

Posted 2011-02-07T20:33:54.897

Reputation: 10 244

how about n%i<1? – st0le – 2011-02-08T09:06:11.573

3

Perl, 42 chars

map{$n/=$_,print$_,$/until$n%$_}2..($n=<>)

ninjalj

Posted 2011-02-07T20:33:54.897

Reputation: 3 018

Using say instead of print$_ let you save 4 chars. – F. Hauri – 2013-10-28T00:20:34.583

3

Windows PowerShell, 46

Naïve solution.

2..($x=+"$input")|%{for(;!($x%$_)){$_;$x/=$_}}

Joey

Posted 2011-02-07T20:33:54.897

Reputation: 12 260

43 bytes: param($x)2..$x|%{for(;!($x%$_)){$_;$x/=$_}} – mazzy – 2018-08-10T15:09:57.753

The number is given on standard input. Read the task first, please. – Joey – 2018-08-11T05:52:07.830

parm($x) is pure way to declare a standard input. Read the Powershell documentation first, please :) – mazzy – 2018-08-11T06:43:36.937

Try it out for yourself: echo 35 | powershell -noprofile -command "param($x)2..$x|%{for(;!($x%$_)){$_;$x/=$_}}". param declares a parameter to a function/script/scriptblock which is given after the invocation, not as pipeline input. Standard input is a well-defined term and refers to one of the default streams available to console programs on many platforms. – Joey – 2018-08-11T07:48:01.300

You don't need to run a PowerShell script as command from cmd.exe. Save the text to a file (test.ps1, for example) and run it from Powershell PS> test.ps1 35 :) – mazzy – 2018-08-11T15:38:09.337

To run a script text (not script file) with paramters you need to use a powershell call operator &. For example & {param($x)2..$x|%{for(;!($x%$_)){$_;$x/=$_}}} 35. If you insist on running a powershell scriptblock from cmd.exe then try powershell -noprofile -command "& {param($x)2..$x|%{for(;!($x%$_)){$_;$x/=$_}}} 35". – mazzy – 2018-08-11T15:38:51.260

$input is one of several automatic variables. You do not have to use only it. There are other ways :) – mazzy – 2018-08-11T15:39:45.643

@mazzy: The point is, that a command-line argument to the script/scriptblock/function is not standard input. – Joey – 2018-08-11T22:35:14.570

(oO) as you wish – mazzy – 2018-08-12T13:06:30.750

2

Pure Bash Solution (60 characters)

Based on F.Hauri's solution (thanks to him):

read p;for((i=2;p-1;));do((p%i++||(p/=--i)*0))||echo $i;done

gniourf_gniourf

Posted 2011-02-07T20:33:54.897

Reputation: 141

*0 great idea! – F. Hauri – 2013-11-03T14:48:00.823

2

Python (55)

based on marcog

n=input()
i=1
while~-n:
 i+=1
 while n%i<1:print i;n/=i

recursive

Posted 2011-02-07T20:33:54.897

Reputation: 8 616

1

C (68)

Not the shortest, but I was curious to see how a C solution could compare.

d=2;main(n){scanf("%d",&n);for(;n>1;)n%d?++d:printf("%d\n",d,n/=d);}

Daniel Lubarov

Posted 2011-02-07T20:33:54.897

Reputation: 301

1Nice. You can also take advantage of the fact that printf accepts a variable number of arguments to hide the n/=d in its argument list. I suspect you could also save a character or two by promoting d to a global variable. – breadbox – 2013-10-28T05:42:55.893

Good ideas! That brings it to 68. – Daniel Lubarov – 2013-10-28T16:33:52.020

1

100% Pure : 72 chars

read p;for((i=1;p>i++;));do while((p%i<1));do echo $i;((p/=i));done;done

or

read p;i=1;while((p>i++));do while((p%i<1));do o+=$i\ ;((p/=i));done;done ;echo $o

This seem longer, but in replacing for by while, I could make an overall loop and using alias to reduce then code:

alias D=done W=while
prime() { W read p;do i=1 o=;W((p>i++));do W((p%i<1));do o+=$i\ ;((p/=i));D;D;echo $o;D;}
unalias D W

This way, my (written) code whith the loop is 77 char length.

Anyway, the function is memorized with full command names:

declare -f prime
prime () 
{ 
    while read p; do
        i=1 o=;
        while ((p>i++)); do
            while ((p%i<1)); do
                o+=$i\ ;
                ((p/=i));
            done;
        done;
        echo $o;
    done
}

F. Hauri

Posted 2011-02-07T20:33:54.897

Reputation: 2 654

Hi @F.Hauri :). Here's a shorter version: read p;for((i=2;p-1;));do((p%i==0&&(p/=i)))&&echo $i||((++i));done. Also, use declare -f prime instead of the ugly set | sed. Cheers :). – gniourf_gniourf – 2013-11-03T13:22:01.830

Even shorter: read p;for((i=2;p-1;));do((p%i&&++i))||{((p/=i));echo $i;};done (63 chars :)). – gniourf_gniourf – 2013-11-03T13:30:11.397

Hi @gniourf_gniourf, thanks for declare -f, sometime, re-reading man pages... Bravo for your shortest bash solution, if you publish I will upvote! – F. Hauri – 2013-11-03T13:58:55.387

1

Mathematica, 26 chars

#&@@@FactorInteger@Input[]

alephalpha

Posted 2011-02-07T20:33:54.897

Reputation: 23 988

1

R, 72 characters

Just for the kicks, an attempt without using any preexisting function to prime-factorize:

n=scan(n=1);m=1:n;M=m[!n%%m&!m%in%c(1,n)];M[rowSums(!outer(M,M,`%%`))<2]

Ungolfed with explanations:

n <- scan(n=1) #Take one number from stdin
m <- 1:n
#Of which of the integers from 1 to n is n a multiple (excluding 1 and himself):
M <- m[!n%%m & !m%in%c(1,n)] 
#Trim that list by excluding integers that are multiples of others in the list:
M[rowSums(!outer(M,M,`%%`))<2]

NB: Instead of checking if n%%m==0, use the fact the 0 coerce as FALSE when using !.

plannapus

Posted 2011-02-07T20:33:54.897

Reputation: 8 610

1

Javascript (54)

n=prompt()*1;for(i=2;n>1;n/=n%i?(++i,1):(alert(i),i));

aviraldg

Posted 2011-02-07T20:33:54.897

Reputation: 189

2You can do prompt()*1 instead of parseInt(prompt()) to convert to an integer. – HoLyVieR – 2011-02-08T22:05:33.893

1+prompt() is shorter. – Casey Chu – 2011-09-14T02:09:35.873

0

perl5.10: 36 chars

map{$p/=$_,say until$p%$_}2..($p=<>)

F. Hauri

Posted 2011-02-07T20:33:54.897

Reputation: 2 654

0

Haskell 74 chars

s n=f((==0).mod n)[2..n]
main=readLn>>=print.f((==1).length.s).s
f=filter

Example

12 -> [2,3]
128 -> [2]
60 -> [2,3,5]

Ray

Posted 2011-02-07T20:33:54.897

Reputation: 1 946

0

Octave, 17 chars

factor(input(''))

MrD

Posted 2011-02-07T20:33:54.897

Reputation: 171

0

Scala 111:

def f(i:Int,c:Int=2):List[Int]=if(i==c)List(i)else
if(i%c==0)c::f(i/c,c)else f(i,c+1)
f(readInt).mkString(" ")

user unknown

Posted 2011-02-07T20:33:54.897

Reputation: 4 210