0
\$\begingroup\$

In this challenge, your job is to write a program that allows a user to enter an integer. Then, the program must find all the possible ways of multiplying two numbers to get the user's integer.
Rules:

  • The program should not show any problem more than once.
  • The user should be able to enter any integer.
  • The program should only show problems that contain 2 numbers.
  • The shortest program (in Bytes) wins.
  • It does not matter what order the program outputs the problems in.

Example Output (user enters 20, then the program outputs the problems):

20

1 * 20
2 * 10
4 * 5

\$\endgroup\$
4
  • 4
    \$\begingroup\$ So basically this is just "factor an integer." predicts ridiculously short Mathematica solution \$\endgroup\$
    – Doorknob
    Jan 18, 2014 at 19:27
  • \$\begingroup\$ @DoorknobofSnow yes but all possible factorizations. \$\endgroup\$
    – wchargin
    Jan 18, 2014 at 19:28
  • 1
    \$\begingroup\$ What do you mean "yes, but all possible factorizations"? That's what factoring is. Unless you mean including non-integer "factors", in which case there are infinite solutions. \$\endgroup\$
    – jscs
    Jan 18, 2014 at 20:23
  • 1
    \$\begingroup\$ This is just factoring, which has been done before: Factorize me!!!, Find prime factors \$\endgroup\$ Jan 18, 2014 at 20:36

6 Answers 6

3
\$\begingroup\$

Perl 6, 54 52 49 characters

my \i=get;i%$_||say $_,"*",i/$_ for 1...^*>i.sqrt

Iterates through each integer from 1 to the first number greater than the square root of the input (* > i.sqrt), excludes that last number (...^), and prints (say $_,"*",i/$_) unless the number doesn't divide the input evenly (i % $_ ||)

\$\endgroup\$
3
\$\begingroup\$

Let's give VIM some love (96 chars)

let n=input('')|for x in range(1,n/2-1)|for y in range(1,n)|if x*y==n|ec x."*".y|en|endfo|endfo

The abbreviations ec, en and endfo stand for echo, endif and endfor respectively.

I was rather disappointed that I couldn't use fo; that expands to fold.

\$\endgroup\$
1
\$\begingroup\$

python, 85 79 75 73 characters

i,j=1,int(raw_input())
while i*i<=j:
    if j%i==0:
        print i,"*",j/i
    i+=1

This can probably be improved drastically...

\$\endgroup\$
2
  • \$\begingroup\$ Pretty good (for Python!) \$\endgroup\$ Jan 18, 2014 at 20:13
  • 2
    \$\begingroup\$ Design for readability really kills in codegolf, but I bet Java can't beat that :P \$\endgroup\$ Jan 18, 2014 at 20:13
1
\$\begingroup\$

JavaScript, 64 characters

Algorithm: run for loop till square root of the input number and if n % i == 0 alert the pair of numbers.

n=prompt();for(i=1;i<Math.sqrt(n);i++)if(!(n%i))alert(i+"*"+n/i)
\$\endgroup\$
0
\$\begingroup\$

C, 75 71 70 characters

n;main(i){scanf("%d",&n);for(;i*i<n;i++)n%i||printf("%d*%d\n",i,n/i);}

Relies on the fact that the program is called with 0 arguments so i is initialized to 1. I am currently beating the python solution, so I am quite estatic. :D

\$\endgroup\$
0
\$\begingroup\$

perl 5 82 characters...

  my $n=20;
  for(1..sqrt($n)){
    say "$_ * ",$n/$_ if $n%$_ == 0;
  }
\$\endgroup\$
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.