Displaying the Prime factors of an integer.

On rebol.org this oneliner script by Sunanda can be found.

I needs slight **mod** ification to work on REN-C. And with a bit of realigning and getting rid of the abbreviation for 'append we arrive at this function.

```
prime-factors: function [n [integer!]][
m: 2
s: 1
a: copy[]
until[
either n mod m = 0 [
n: n / m
append a m
][
m: m + s
s: 2
]
if 1. * m * m > n [
append a n
n: 1
]
n = 1
]
a
]
```

Where it is to be noted that n should be >= 2 and the maximum size will also be limited

1 Like

Four years later, I've added this to the tests.

Small changes needed:

```
if 1. * m * m > n [
append a n
n: 1
]
```

A new thing is that `1.`

is a tuple now. If you want the decimal number, you have to write `1.0`

```
>> length of 1.
== 2
>> first 1.
== 1
>> second 1.
== _
>> to block! 1.
== [1 _]
```

The rationale behind this is that the dialecting part is more useful. If you want to write something like:

```
my-dialect: [
1. {First things first}
2. {Second things second}
]
```

Then having them be TUPLE! preserves the notation. If they were LOAD-ed as decimal numbers then they would be canonized as 1.0 and 2.0 in subsequent molds, and you'd lose the distinction between 1. and 1.0

(By the same token, **.1** and **.2** are also TUPLE!, and if you want the decimals you should write 0.1 and 0.2)

Also the locals-gathering FUNCTION is deprecated. There are simply too many dialect purposes for SET-WORD! that should not make locals. So explicit `<local>`

definitions or LETs are needed:

```
prime-factors: func [n [integer!]][
let m: 2
let s: 1
let a: copy[]
...
```

Eventually, FUNC is scheduled to be a synonym for FUNCTION.

2 Likes