#!/usr/bin/perl

eval 'exec perl -S $0 ${1+"$@"}'
    if 0;

# Factorises large numbers. Requires a quantum computer.

# The input to the program is a number divided into 16 bit words, from least
# significant word to most, preceded by the word count. For example, 131069
# - which is 65536 + 65533 - is entered as:

#    TWO
#    SIX FIVE FIVE THREE THREE
#    ONE

# That is: two words, LSW is 65533, MSW is 1.

# The output is a sequence of factors, in the same format, but with their
# words interleaved. For example, the output with the above input is:

# 1    I
# 2    LIII
# 3    MMCDLXXIII
# 4    I
# 5    MMCDLXXIII
# 6    LIII
# 7    II
# 8    lxvDXXXIII
# 9    I
#10    I
#11    NIHIL
#12    II
#13    I
#14    lxvDXXXIII
#15    NIHIL
#16    I

# This means that 4 factorisations have been found. The first one (first three
# lines) is 53 * 2473 (both 1 word numbers). The second one is 2473 * 53.
# The third factorisation (lines 7 to 11) is composed of two word numbers,
# the first number (lines 8 and 10) is 131069 and the second number (lines 9
# and 11) is 1. Similarly for the last factorisation.

# The program assumes that the input is a product of two primes -- in that
# case, it will print the four possible factorisations (p*q, q*p, 1*pq, pq*1)
# If the number has more factors, not all possible factorisations are printed,
# but you can just run it again after selecting one of the numbers from the
# output.

# Note that factorisation of large numbers using the algorithm implemented
# here is unrealistic without a quantum computer, although the quantum emulator
# will try its best.

# This file is part of CLC-INTERCAL.

# WARNING - do not operate heavy machinery while using CLC-INTERCAL

# Copyright (C) 1999 Claudio Calvelli <lunatic@assurdo.com>, all rights reserved

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

use Language::INTERCAL 'factor', '
	@z

	K@~@@@@@@
	K@~@@@@@
	K@~@@@@@@@@@@@
	K@~@@@@@@K
	K@~@@@@@@
	K@~@@@@@@@@

	k@~@@
	k@~@@
	k@~@
	k@~@@@

	@@@@@@@@z

	@@~@@@@@@
	@@@k@@@@

	@@@@@@@@@z

	@@~@@@@@@@
	@@k@@@@@k@@@@

	````````````````````````````````````````````````````````````````

	@@{@@M]@@@|
	@@K@@@{
	@@K@@@{
	@@K@@@{

	@@{@@M]@@@|
	@@k@@@{
	@@k@@@{

	@@{@@M]@@@|

	@@{@@M]@@@|
	@@K@@@{

	@z

	````````````````````````````````````````````````````````````````

	@z@@@@@@

	````````````````````````````````````````````````````````````````

	@@@K
	@k@L`@K
	@K@L`@{
	@@@K
	@K@@{
	@@@K
	@k@@K@L`@K
	@K@L`@eK@J@K@@{@J@{
	@K@L`@K@@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K

	@z

	````````````````````````````````````````````````````````````````

	@z@@@k@k@@

	````````````````````````````````````````````````````````````````

	@K@L`@{
	@@@M]
	@K@L`@}k@@K}@@K
	@K@L`@e}K@@K}@J@{@@K
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K
M]	@K@L`@}K@J@{}@@}{@J@{}
	@@@K
	@k@L`@K
	@k@L`@K
	@k@L`@K
	@K@L`@{
	@k@@{@L`@{
	@k@@{@L`@{
M]	@k@@{@L`@{
	@@@K
	@K@@{
	@k@@{@L`@{
	@k@@{@L`@{
	@k@@{@L`@{
	@@@M]
	@K@L`@eK@J@K@@{@J@{
	@K@L`@K@@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K
	@K@L`@{
	@K@L`@{
	@K@L`@{
	@K@L`@{
	@K@L`@{
	@K@L`@{

	@z

	````````````````````````````````````````````````````````````````

	@z@@@@@@@@
	@@@

	````````````````````````````````````````````````````````````````

	@@@M]
	@@@K
	@K@L`@{
	@K@L`@eK@J@K@@{@J@{
	@K@L`@K@@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K
	@K@L`@}k@@K}@@K
	@K@L`@K@@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K
	@@@M]
	@K@L`@eK@J@K@@{@J@{
	@K@L`@eK@@K@@{@J@{@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K
	@K@@{
	@K@L`@ek@@K@J@k@@K@@{@J@{
	@K@L`@K@@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K
M]	@@@

	@@@K
	@K@L`@{
	@K@@K@@Ka\
M]	@@@K
	@@@K
	@@@K
	@@

	@@@K
	@K@L`@PekKJkK{J{JK{J{K
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{

	@z

	````````````````````````````````````````````````````````````````

	@z@@@@@@@@@@M@]
	@@M@]

	````````````````````````````````````````````````````````````````

	@K@L`@{
	@@K@N@K@N@K@N@K@N@K@N@K@N@K
		N@K@N@K@N@K@N@K@N@K@N@K@N@K@N@K@N@K
		N@K@N@K@N@K@N@K@N@K@N@K@N@K@N@K
		N@K@N@K@N@K@N@K@N@K
		N@K@N@K@N@K@N@K@N@K@N@K
		N@K@N@K@N@K
		N@K@N@K@N@K
		N@k@N@k@N@k@N@|@N@|@N@|
	@@
	@K@L`@{
M]	@@K@N@K@N@K@N@K@N@K@N@K@N@K
		@@N@K@N@K@N@K@N@K@N@K@N@K@N@K@N@K@N@K
		@@N@K@N@K@N@K@N@K@N@K@N@K@N@K@N@K
		@@N@K@N@K@N@K@N@K@N@K
		@@N@K@N@K@N@K@N@K@N@K@N@K
		@@N@K@N@K@N@K
		@@N@K@N@K@N@K
		@@N@k@N@k@N@k@N@|@N@|@N@|

	@z

	````````````````````````````````````````````````````````````````

	@z@@@@@K@@@\@@K@@k
	\@

	````````````````````````````````````````````````````````````````

M]	@K@L`@{@@eK@J@{@@{
	@k@@{
M]	@k@@K@L`@k@@K@J@K@@{@J@{

	@z

	````````````````````````````````````````````````````````````````

	@z@@@@@K@@@\@@K@@k
	\@

	````````````````````````````````````````````````````````````````

	@@@K
	@k@@{
	@k@@K@L`@k@@K@J@K@@{@J@{
M]	@K@L`@{@@K
	@k@@{
M]	@k@@K@L`@k@@K@J@K@@{@J@{

	@z

	````````````````````````````````````````````````````````````````

	@z@@@@@@@@@

	````````````````````````````````````````````````````````````````

	@@@K
	@@@M]
	@@@K
	@@@M]
	@K@@{
	@K@L`@K@J@{@@{@J@{
	@K@L`@K@@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K
	@K@L`@eK@J@K@@{@J@{
	@K@L`@K@@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K
	@@
	@@@K
	@K@L`@{
	@K@L`@{
M]	@K@@{

	@z

	````````````````````````````````````````````````````````````````

	@@@@@@@@M@]

	````````````````````````````````````````````````````````````````

M]	@@K@N@K@N@K
	@K@L`@[|@@{
	@K@L`@{
	@@@M]
	@K@L`@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K
	@[|@L`@e@K@J@[|@@{@@{@J@{
	@@K@N@K@N@K
	@@
	@@@K
	@K@L`@{
	@K@L`@K@@{
M]	@K@L`@K@J@{@@{@J@{

	@z

	````````````````````````````````````````````````````````````````

	@@@@@@@k@@@@@
	@@@@MK`]@@@K@

	````````````````````````````````````````````````````````````````

M]	@@K@N@K@N@K@N@K@N@K@N
		@@@@@K@N@K@N@K
	@K@L`@K
	@K@L`@{
	@@K@@@{
	@@K@@@{
	@@K@@@{
	@@K@@@{
	@@K@@K
	@@K@@K
M]	@K@L`@{
	@@@K
	@K@@{
	@K@@{
	@@@M]
	@K@L`@{
	@K@L`@[|@@K
	@K@L`@{
	@@@M]
	@K@L`@eK@J@K@@{@J@{
	@K@L`@K@@K@@{
	@K@L`@eK@J@{@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K
	@K@@{
	@K@L`@K@J@K@@{@@{@J@{
M]	@K@L`@K@J@{@@{@J@{
	@@@K
	@K@L`@K@J@K@@{@J@{
	@K@L`@k@@K
	@K@@{
	@k@@K@L`@K
	@K@L`@K
	@K@L`@eK@J@K@@{@J@{
	@K@L`@K@@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K
	@@K@N@K@N@K@N@K@N@K@N
			K@N@K@N@K
	@@

	@z

	````````````````````````````````````````````````````````````````

	@@@@@@@}@@@@K
	@@@k@@@}@@K

	````````````````````````````````````````````````````````````````

M]	@@K@N@K@N@K@N@K@N@K
	@K@L`@{
	@K@L`@{
	@@@K
	@K@L`@PP[[|@J@[|@@{@J@{@J@P[[|@J
		@@@@@K@@{@J@{@@{@J@{@J@P[|@J
		@@@@@K@@{@J@{@@{@J@{@J@K@
		@@@@@{@J@{
	@[|@L`@e[|@J@P@e[[|@J@K@@{@J@{@J
		@@@@@K@@{@J@{@@{@J@{
	@K@L`@K@J@{@@{@J@{
	@K@L`@K@J@{@@{@J@{
	@K@L`@K@@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K
	@K@L`@eK@@{@J@{@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K
	@K@L`@[|
	@@K@@@{
	@K@@{
	@[|@L`@K
	@@@K
	@@K@N@K@N@K@N@K@N@K
	@@

	@z

	````````````````````````````````````````````````````````````````

	@@@@@@@@
	@@@@@@@@@K

	````````````````````````````````````````````````````````````````

M]	@@@K
	@K@L`@{
	@@@K
	@K@@{
	@K@L`@k@@K
	@@@K
	@K@L`@k@@K
	@@@K
	@K@L`@eK@J@K@@{@J@{
	@K@L`@K@@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
	@K@L`@K@J@K@@{
M]	@K@L`@{@@K
	@K@L`@{
	@K@L`@KaK
	@@
', (@ARGV == 1 && lc($ARGV[0]) eq 'list' ? (\*STDOUT) : ()), 'nobug';
(@ARGV != 1 or lc($ARGV[0]) ne 'list') and factor;

