user
Task

Compute the   factors   of a positive integer.

These factors are the positive integers by which the number being factored can be divided to yield a positive integer result.

(Though the concepts function correctly for zero and negative integers, the set of factors of zero has countably infinite members, and the factors of negative integers can be obtained from the factors of related positive numbers without difficulty;   this task does not require handling of either of these cases).

Note that every prime number has two factors:   **1**   and itself.

Related tasks

* count in factors
* prime decomposition
* Sieve of Eratosthenes
* primality by trial division
* factors of a Mersenne number
* trial factoring of a Mersenne number
* partition an integer X into N primes
* sequence of primes by Trial Division
* sequence: smallest number greater than previous term with exactly n divisors
assistant
(formerly Perl 6)

**Works with**: Rakudo version 2015.12

### Brute-force

Naive, brute-force, and slow, but ok for small numbers.

```
sub factors (Int $n) { (1..$n).grep($n %% *) }
```

### Optimized

If you don't want to roll your own.

```
use Prime::Factor;

put divisors :s, 2⁹⁹ + 1;

say (now - INIT now).round(.001) ~' seconds';
```

Output:

```
1 3 9 19 27 57 67 171 201 513 603 683 1273 1809 2049 3819 5347 6147 11457 12977 16041 18441 20857 34371 38931 45761 48123 62571 101593 116793 137283 144369 187713 304779 350379 358249 396283 411849 563139 869459 914337 1074747 1188849 1235547 1397419 2608377 2743011 3224241 3566547 3652001 4192257 6806731 7825131 9672723 10699641 10956003 12576771 14245331 20420193 23475393 26550961 32868009 37730313 42735993 61260579 69388019 79652883 98604027 111522379 128207979 183781737 208164057 238958649 244684067 270661289 334567137 384623937 624492171 716875947 734052201 811983867 954437177 1003701411 1873476513 2118925201 2202156603 2435951601 2863311531 3011104233 4648997273 6356775603 6606469809 7307854803 7471999393 8589934593 13946991819 18134306363 19070326809 22415998179 25769803779 41840975457 54402919089 57210980427 67247994537 76169784857 125522926371 141967988467 163208757267 201743983611 228509354571 425903965401 489626271801 685528063713 1277711896203 1447225912283 2056584191139 3833135688609 4341677736849 5103375585419 13025033210547 15310126756257 39075099631641 45930380268771 96964136122961 137791140806313 242099935645987 290892408368883 726299806937961 872677225106649 2178899420813883 2618031675319947 4599898777273753 6536698262441649 13799696331821259 16220695688281129 41399088995463777 48662087064843387 124197266986391331 145986261194530161 165354256046209121 308193218077341451 437958783583590483 496062768138627363 924579654232024353 1294508355899092489 1488188304415882089 2773738962696073059 3141730864877973299 3883525067697277467 4464564913247646267 5049478357768350859 8321216888088219177 9425192594633919897 11078735155096011107 11650575203091832401 15148435073305052577 24595658762082757291 28275577783901759691 33236205465288033321 34951725609275497203 45445305219915157731 73786976286248271873 84826733351705279073 86732059845239196763 95940088797598666321 99708616395864099963 136335915659745473193 210495967946824211033 221360928858744815619 260196179535717590289 287820266392795998963 299125849187592299889 338315049970479507553 631487903840472633099 664082786576234446857 780588538607152770867 863460799178387996889 884149207079080169987 1014945149911438522659 1647909137059544738497 1894463711521417899297 2341765615821458312601 2590382397535163990667 2652447621237240509961 3044835449734315567977 3448793718355783636697 4943727411178634215491 5683391134564253697891 6427985949439110643507 7957342863711721529883 9134506349202946703931 10346381155067350910091 14831182233535902646473 16798834934502523229753 19283957848317331930521 23872028591135164589649 26999560778987372043073 31039143465202052730273 44493546700607707939419 50396504803507569689259 57851873544951995791563 59237996874298371389129 65527080648759889097243 80998682336962116129219 93117430395606158190819 151189514410522709067777 173555620634855987374689 177713990622895114167387 196581241946279667291729 231069179129837503658699 242996047010886348387657 453568543231568127203331 512991654800760068818387 533141971868685342502161 589743725838839001875187 693207537389512510976097 728988141032659045162971 1125521940611669056393451 1538974964402280206455161 1599425915606056027506483 1769231177516517005625561 1808970572192153926885891 2079622612168537532928291 3376565821835007169180353 4390314403466912569515281 4616924893206840619365483 5426911716576461780657673 6238867836505612598784873 10129697465505021507541059 13170943210400737708545843 13850774679620521858096449 16280735149729385341973019 18440700012048375105418859 30389092396515064522623177 34370440871650924610831929 39512829631202213125637529 48842205449188156025919057 55322100036145125316256577 103111322614952773832495787 118538488893606639376912587 165966300108435375948769731 309333967844858321497487361 350373300228919127002958321 497898900325306127846309193 928001903534574964492462083 1051119900686757381008874963 1235526900807241132063063553 3153359702060272143026624889 3706580702421723396189190659 9460079106180816429079874667 11119742107265170188567571977 23475011115337581509198207507 33359226321795510565702715931 70425033346012744527594622521 211275100038038233582783867563 633825300114114700748351602689
0.046 seconds
```