Consulting

Results 1 to 4 of 4

Thread: VBA Mac coding Question

  1. #1
    VBAX Newbie
    Joined
    Feb 2021
    Posts
    1
    Location

    VBA Mac coding Question

    Trying to write code to show prime numbers 3-1000, then show the sum of the digits of each of those prime numbers, and ultimately just show the prime numbers whose sum is prime. Example the number is 11 to show 1+1=2, 2 would also be shown because it is also a prime number. I have the code working for prime numbers, however cannot get it to sum the digits or show the summed primes. Please help!

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,391
    Location
    Have you tried this?
    =SUMPRODUCT(--MID(A1,ROW(INDIRECT("1:" & LEN(A1))),1))
    Last edited by Aussiebear; 03-22-2025 at 02:34 PM.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    I see this was posted a few months ago. Are you still working on the problem? If so, you mention the sum of digits of 11 is 2 and 2 is prime. Are you summing all the digits of all the primes or only the sum of the digits that result in a prime sum? Are you doing this all in VBA or are you displaying all primes <1000 on a spreadsheet?

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,391
    Location
    Okay after a little homework...

    This code will print Prime Numbers from 3 to 1000 in column A

    Sub FindPrimesToColumnA()
      Dim i As Long
      Dim j As Long
      Dim IsPrime As Boolean
      Dim RowNum As Long
      RowNum = 1 ' Start writing primes from cell A1
      Cells(RowNum, 1).Value = 2 '2 is the first prime
      RowNum = RowNum + 1
      For i = 3 To 1000 Step 2
        IsPrime = True
        For j = 3 To Sqr(i) Step 2
          If i Mod j = 0 Then
            IsPrime = False
            Exit For
          End If
        Next j
        If IsPrime Then
          Cells(RowNum, 1).Value = i ' Write the prime number to column A
          RowNum = RowNum + 1
        End If
      Next i
    End Sub
    This next set of codes should produce the sum of umbers to make each Prime.


    Sub PrimeSums()
      Dim i As Long, j As Long, k As Long
      Dim IsPrime As Boolean
      Dim RowNum As Long
      RowNum = 1
      Cells(RowNum, 1).Value = 2 '2 is the first prime
      RowNum = RowNum + 1
      For i = 3 To 1000 Step 2
        IsPrime = True
        For j = 3 To Sqr(i) Step 2
          If i Mod j = 0 Then
            IsPrime = False
            Exit For
          End If
        Next j
        If IsPrime Then
          Cells(RowNum, 1).Value = i 'Write prime number
          Cells(RowNum, 2).Value = FindSumCombinations(i) 'Write sum combinations
          RowNum = RowNum + 1
        End If
      Next i
    End Sub
    
    
    Function FindSumCombinations(target As Long) As String
      Dim a As Long, b As Long
      Dim result As String
      result = ""
      For a = 2 To target - 2 'Start from 2
        For b = a To target - a 'Start from a to avoid duplicates
          If a + b = target Then
            If result <> "" Then
              result = result & "; " 'Separate combinations with semicolon
            End If
            result = result & a & "+" & b
          End If
        Next b
      Next a
      FindSumCombinations = result
    End Function
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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