Excel excel problem, prime numbers :: MrExcel Message Board


 FAQFAQ
   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   FavoritesFavorites   StatisticsStatistics 
 RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Online StoreOnline Store

MrExcel Message Board Forum Index -> Excel Questions

excel problem, prime numbers
Post new topic   Reply to topic
Last Thread | Next Thread  >   Printable version
  Author    Thread

mwilliams232
Welcome to the Board


Joined: 09 Dec 2002
Posts: 2


Status: Offline

 Reply with quote  

hey,

does anyone know how to make an excel spreadsheet that inputs any number and in another cell outputs if the number is prime or not. thanks.

-matt w.

Post Mon Dec 09, 2002 9:22 am 
 View user's profile Send private message

Ken.Zeng
Welcome to the Board


Joined: 03 Dec 2002
Posts: 1


Status: Offline

 Reply with quote  

U can use
countif
=IF(COUNTIF(Myrange,A1)=1,"Prime",COUNTIF(Myrange,A1))
Myrange=Sheet2!$A$2:INDIRECT("A"&COUNTA(Sheet2!$A:$A))
Input you NO.columnA
Input the above formula in cell A1
then paste down



[ This Message was edited by: Ken.Zeng on 2002-12-09 04:36 ]

Post Mon Dec 09, 2002 9:33 am 
 View user's profile Send private message

mwilliams232
Welcome to the Board


Joined: 09 Dec 2002
Posts: 2


Status: Offline

 Reply with quote  

when i input that formula into A1, it comes up as #NAME? do you know what is wrong?

thanks

Post Mon Dec 09, 2002 9:41 am 
 View user's profile Send private message

Richie(UK)
MrExcel MVP


Joined: 18 May 2002
Posts: 2425
Location: Worcester, England
Flag: Uk

Status: Offline

 Reply with quote  

Hi Matt,

Paste this into a general module in the workbook you are dealing with.

Function Prime(TestRng As Range)

'code by Tom Ogilvy (modified from original by Myrna Larson)
'slight modification by RPS to accept range
Dim PrimeCnt As Long
Dim y As Long
Dim x As Long
Dim i As Integer
Dim Flag As Boolean
Dim Primes() As Long
Dim NumStop As Double
Dim TestNum As Long
ReDim Primes(1 To 2)

TestNum = TestRng.Value

NumStop = Sqr(TestNum)
If TestNum = 1 Or _
TestNum = 2 Or _
TestNum = 3 Or _
TestNum = 5 Then
Prime = True
Exit Function
End If
Primes(1) = 2
Primes(2) = 3
PrimeCnt = 2
x = 3

Do
x = x + 2
For y = 3 To Sqr(x) Step 2
If x Mod y = 0 Then GoTo NoPrime1
Next y
PrimeCnt = PrimeCnt + 1
ReDim Preserve Primes(1 To PrimeCnt)
Primes(PrimeCnt) = x
NoPrime1:
Loop Until Primes(PrimeCnt) > NumStop

For i = LBound(Primes) To UBound(Primes)
If TestNum Mod Primes(i) = 0 Then
Debug.Print i, Primes(i), TestNum / Primes(i)
Prime = False
Exit Function
End If
Next
Prime = True
End Function

Then to see if the value in A1 is prime, type "=prime(A1)" in your chosen cell.

HTH

_________________
Richie

Post Mon Dec 09, 2002 10:14 am 
 View user's profile Send private message

Andrew Poulsom
MrExcel MVP


Joined: 22 Jul 2002
Posts: 8180

Flag: Uk

Status: Offline

 Reply with quote  

Here are two more custom functions:

code:

Option Explicit

'' ***************************************************************************
'' Purpose  : Test whether the argument is a PRIME number
'' Written  : January 98 by Andy Wiggins - Byg Software Ltd
'' Notes    : Returns TRUE or a zero length string
''
Function IsPrime(vlTestNumber As Long)
''Application.Volatile                                ''Not essential for this demo
Dim vlCount As Long                                 ''Declare some variables
Dim vlHalf As Long

    vlHalf = vlTestNumber / 2 + 1                   ''By using a LONG we get an integer result, which is what we want
    For vlCount = 2 To vlHalf                       ''Loop until we get to vlHalf
        If (vlTestNumber Mod vlCount) = 0 Then      ''If we get a ZERO result in this test, the number is not a prime ..
            IsPrime = ""                            ''.. so return an empty string ..
            Exit Function                           ''.. and leave the function
        End If
    Next
    IsPrime = True                                  ''If we get here, the number is a prime, so return TRUE
End Function

'' ***************************************************************************
'' Purpose  : Test whether the argument is a PRIME number
'' Written  : January 98 by Andy Wiggins - Byg Software Ltd
'' Notes    : Returns TRUE or FALSE
''
Function IsPrimeBool(vlTestNumber As Long) As Boolean
''Application.Volatile                                ''Not essential for this demo
Dim vlCount As Long
Dim vlHalf As Long

    vlHalf = vlTestNumber / 2 + 1                   ''By using a LONG we get an integer result, which is what we want
    For vlCount = 2 To vlHalf                       ''Loop until we get to vlHalf
        If (vlTestNumber Mod vlCount) = 0 Then      ''If we get a ZERO result in this test, the number is not a prime ..
            IsPrimeBool = False                     ''.. so return FALSE
            Exit Function                           ''.. and leave the function
        End If
    Next
    IsPrimeBool = True                              ''If we get here, the number is a prime, so return TRUE
End Function




And here is an array formula:

=OR(A1=1,A1=3,ISNA(MATCH(TRUE,A1/ROW(INDIRECT("2:"&INT(SQRT(A1))))=INT(A1/ROW(INDIRECT("2:"&INT(SQRT(A1))))),0)))

After typing it press Ctrl+Shift+Enter not just Enter.

Post Mon Dec 09, 2002 10:32 am 
 View user's profile Send private message

Aladin Akyurek
.


Joined: 15 Feb 2002
Posts: 13745
Location: The Hague
Flag: Blank

Status: Offline

 Reply with quote  

And more here...

http://www.mrexcel.com/board/viewtopic.php?topic=17861&forum=2

Post Mon Dec 09, 2002 11:56 am 
 View user's profile Send private message
  Display posts from previous:      

MrExcel Message Board Forum Index -> Excel Questions


Forum Jump:
Jump to:  

Post new topic   Reply to topic
Page 1 of 1



Add To Favorites

 


Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB: 2.0.4 © 2001 phpBB Group

Need help posting your first question? Read how to post

Need extra help ? Couldn't get the answer you needed ? Get a free quote from our Consulting Team

Download Colo's HTML Maker utility for displaying your Excel Worksheet on the board.

Download VB HTML Maker to post your code on the board


Check out our new index to 485 Excel Articles.


Return to MrExcel Consulting

All contents Copyright 1998-2004 by MrExcel.com
If you believe information posted here is from your copyrighted source, notify us per the Terms of Use
Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.