# Smallest prime factor of a number

1. ## Smallest prime factor of a number

Hi,
Is there any formula witch i can use to find the smallest prime factor of a number?

Example: In A1 i write 30 (30=2*3*5 (the prime factors))
in B1 i write a formula, and the result will be 2 (smallest prime factor)

What is the formula?

2. ## Re: Smallest prime factor of a number

dont think there is a formula
this code when inserted in a standard module and run can be used to get all the prime factors of a given number
code from http://support.microsoft.com/kb/202782 it works ok in 2003 and prob 2007
``Please Login or Register  to view this content.``

3. ## Re: Smallest prime factor of a number

I agree with Martin... there are a few functions out there

Determining the smallest factor is relatively straight forward:

=SMALL(IF(MOD(\$A\$1,ROW(INDIRECT("A2:A"&\$A\$1)))=0,ROW(INDIRECT("A2:A"&\$A\$1))),1)
committed with CTRL + SHIFT + ENTER
(k can be changed obviously)

However, refining the above to include only primes as is your requirement is where it gets pretty intensive and the UDF's come into play... if MS has a UDF on their site it's a good indicator that that is the best way to go
(that said the `big hitters` may have something up their respective sleeves)

4. ## Re: Smallest prime factor of a number

The smallest factor is always a prime number.

I'd use this version to allow larger numbers

=MIN(IF(MOD(A1,ROW(INDIRECT("2:"&INT(A1^1/2)))),"",ROW(INDIRECT("2:"&INT(A1^1/2)))))

confirmed with CTRL+SHIFT+ENTER

where your number is in A1

if formula returns 0 then A1 itself is prime...

5. ## Re: Smallest prime factor of a number

Thanks both of you, i will use the both theories

EDIT: i am sorry i thank all 3 of you

6. ## Re: Smallest prime factor of a number

dll that formula falls down in excel<2007 as i presume its using row count
for say
1847 *1861 = 3437267

both primes
formula gives ref

7. ## Re: Smallest prime factor of a number

Martin, thanks

I made a mistake with the formula which doesn't affect the authenticity of any results but restricts the size of the number in A1.

I used A1^1/2 which is, in effect, just the same as A1/2. There should be parentheses around 1/2 or you can use 0.5 instead, i.e.

=MIN(IF(MOD(A1,ROW(INDIRECT("2:"&INT(A1^0.5)))),"",ROW(INDIRECT("2:"&INT(A1^0.5)))))

That should then work for any integer value of A1 up to 268435455 (in any version of Excel). After that an error of the MOD function kicks in (I'm not sure if the MOD error applies in Excel 2007).

8. ## Re: Smallest prime factor of a number

now that works a treat! nice one

There are currently 1 users browsing this thread. (0 members and 1 guests)