Announcement

Collapse
No announcement yet.

CODE GOLF - April 2016 Answers

Collapse
This topic is closed.
X
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • CODE GOLF - April 2016 Answers



    Please post your answers for the CODE GOLF (April 2016) competition in this thread.

    The questions for this competition can be found in the April 2016 newsletter. [click here to view newsletter]

    All answers will be soft deleted once posted and the thread will be locked on Thursday 12 May 2016 to prevent further answers.

    Good Luck!
    Last edited by Dave Hawley; April 24th, 2016, 14:33.

  • #2
    Re: CODE GOLF - April 2016 Answers

    OK, this is my first go... Seems to work.

    Code:
    Public Function IsPrime(varNum As Long) As Boolean
        If varNum < 4 And varNum > 0 Then
            IsPrime = True
            Exit Function
        Else
            If varNum Mod 2 = 0 Then
                IsPrime = False
                Exit Function
            Else
                For x = 3 To (varNum + 1) / 2
                    If varNum Mod x = 0 Then
                        IsPrime = False
                        Exit Function
                    End If
                Next x
            End If
            IsPrime = True
        End If
    End Function

    Comment


    • #3
      Re: CODE GOLF - April 2016 Answers

      Code:
      Public Function IsPrime(varNum As Long) As Boolean
              For i = 2 To varNum - 1
                  If (varNum Mod i = 0) Then
                      IsPrime = False
                      Exit Function
                  End If
              Next
              IsPrime = IIf((varNum > 9 And (InStr(Right$(varNum, 1), "024568"))) Or (varNum < 2), False, True)
      End Function

      Comment


      • #4
        Re: CODE GOLF - April 2016 Answers

        Realised on looking at the newsletter agin that I missed an important detail in my VBA function submission (changing one character would fix it and I am sure I was taught that 1 is a prime number!), I thought I would have a go at the formula challenge as well.

        This will work for row 2, obviously fill down to pick up the relative cell references.

        Code:
        =IF(ISERROR(FIND(" ",A2,FIND(" ",A2)+1)),"",MID(A2,FIND(" ",A2)+1,1))

        Comment


        • #5


          Re: CODE GOLF - April 2016 Answers

          With text in F7:

          =IF(LEN(F7)-LEN(SUBSTITUTE(F7," ",""))<2,"",MID(F7,FIND(" ",F7)+1,1))

          Comment

          Working...
          X