+ Reply to Thread
Results 1 to 21 of 21

Prime and Composite Numbers

  1. #1
    Registered User
    Join Date
    11-11-2019
    Location
    Australia
    MS-Off Ver
    15.0.5179.1000
    Posts
    14

    Prime and Composite Numbers

    Hi all,

    I have a spreadsheet see attached, where I have numbers in columns C to H. In columns K to P I have a formula that tells me if the numbers in columns C to H are prime Numbers or Composite Numbers. The problem I have is, for most of the cells the formula is correct for prime and composite, but a few cells have composite when it should have prime or prime when it should be composite.

    The array formula I'm using is:

    =IF(C2=2,"Prime",IF(OR(MOD(C2,{2,3,5,7,9})=0),"Composite","Prime"))
    Attached Files Attached Files

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,980

    Re: Prime and Composite Numbers

    Do us a favour, please, and indicate the cells we need to look at with a yellow fill. Thanks.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Registered User
    Join Date
    11-11-2019
    Location
    Australia
    MS-Off Ver
    15.0.5179.1000
    Posts
    14

    Re: Prime and Composite Numbers

    Hi Ali the cells you need to look at are K to P

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,980

    Re: Prime and Composite Numbers

    Which specific cells in K and P?

  5. #5
    Registered User
    Join Date
    11-11-2019
    Location
    Australia
    MS-Off Ver
    15.0.5179.1000
    Posts
    14

    Re: Prime and Composite Numbers

    I have uploaded a new copy with the problem highlighted yellow
    Attached Files Attached Files

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,980

    Re: Prime and Composite Numbers

    Try this:

    =IF(C2=2,"Prime",IF(MOD(C2,{2,3,5,7,9})=0,"Composite","Prime"))

    ... confirmed by pressing CTRL+SHIFT+ENTER to activate the array, not just ENTER. You will know the array is active when you see curly braces { } appear around your formula. If you do not CTRL+SHIFT+ENTER you will get an error or a clearly incorrect answer.

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Prime and Composite Numbers

    A couple that I have seen are K10 and K12, where you are testing if 7 is prime by testing if 7 mod 7 is 0 -- which it is. Your test decides that 7 is composite because 7 is divisible by itself. Your algorithm is incomplete and that's why it fails.

    This looks like something for lottery analysis, so you are limited to the numbers 1 to 45 or so? If I were doing this, rather than try to build a robust prime number sieve (search "sieve of eratosthenes" if you want to see how the ancient Greeks figured out how to do it), I would probably just make a list of all the prime numbers between 1 and 45, then use a formula that checks if my number is in the list.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,980

    Re: Prime and Composite Numbers

    I think you might have missed post #6.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    11-11-2019
    Location
    Australia
    MS-Off Ver
    15.0.5179.1000
    Posts
    14

    Re: Prime and Composite Numbers

    Thanks Ali it did not work, although it fixed the first problem I highlighted for you, it has now changed others, if you look at row 144 you will see I've highlighted number 25 as showing prime, however it's a composite number.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    11-11-2019
    Location
    Australia
    MS-Off Ver
    15.0.5179.1000
    Posts
    14

    Re: Prime and Composite Numbers

    I don't understand what MrShorty is saying, I have reach my limits of knowledge on excel. Can someone please correct my formula to make it work properly please.

  11. #11
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,980

    Re: Prime and Composite Numbers

    That's what we are trying to do.

  12. #12
    Registered User
    Join Date
    11-11-2019
    Location
    Australia
    MS-Off Ver
    15.0.5179.1000
    Posts
    14

    Re: Prime and Composite Numbers

    Ok thank you

  13. #13
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,980

    Re: Prime and Composite Numbers

    The problem with the array is that it's not working. I am looking for an alternative.

  14. #14
    Registered User
    Join Date
    11-11-2019
    Location
    Australia
    MS-Off Ver
    15.0.5179.1000
    Posts
    14

    Re: Prime and Composite Numbers

    Thanks Ali I appreciate your help

  15. #15
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,448

    Re: Prime and Composite Numbers

    Try =IF(OR(C2=1;C2=2;C2=3);"prime";IF(AND((MOD(C2;ROW(INDIRECT("2:"&C2-1)))<>0));"prime";"Composite")) and pull down and right as required (replace semi colons with commas if needed)
    in K2 entered as an array formula

  16. #16
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,980

    Re: Prime and Composite Numbers

    Very odd - sometimes your formula is working for 2, sometimes not.

  17. #17
    Registered User
    Join Date
    11-11-2019
    Location
    Australia
    MS-Off Ver
    15.0.5179.1000
    Posts
    14

    Re: Prime and Composite Numbers

    Hi Pepe Le Mokko, that worked thank you I did have to replace the semi Colin with commas. thanks so much for your help and thanks to all for your replies and help.

  18. #18
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,980

    Re: Prime and Composite Numbers

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  19. #19
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Prime and Composite Numbers

    Quote Originally Posted by Pepe Le Mokko View Post
    Try =IF(OR(C2=1;C2=2;C2=3);"prime";IF(AND((MOD(C2;ROW(INDIRECT("2:"&C2-1)))<>0));"prime";"Composite"))
    Why not replace what I highlighted in red above with the following?

    C2<4

  20. #20
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,448

    Re: Prime and Composite Numbers

    I forgot to mention that the solution I proposed was found on the site of the late C Pearson.
    Thank you Chuck !

  21. #21
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,980

    Re: Prime and Composite Numbers

    Chuck? Do you mean Chip?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Prime numbers macro
    By kaue in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-17-2020, 09:08 AM
  2. using excel VBA find and higlight in red all prime numbers in a table of numbers
    By TeraFumba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-10-2017, 07:30 AM
  3. macro to print prime numbers from 1 to 200
    By rarementality in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-02-2014, 02:53 AM
  4. Highlight all prime numbers in column
    By bluecat2013 in forum Excel General
    Replies: 3
    Last Post: 02-21-2014, 01:19 PM
  5. [SOLVED] Array for Finding Prime numbers in a cell
    By Karnik in forum Excel General
    Replies: 6
    Last Post: 11-11-2012, 08:54 PM
  6. Prime numbers
    By Gearcutter in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-08-2007, 09:03 AM
  7. Prime Numbers
    By Flyone in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-02-2005, 10:25 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1