xah icon

0012 - Project Euler

xah | PRO | 11/23/21 07:55:17 AM UTC | 0 ⭐ | 15675 👁️ | Never ⏰ | []
Go |

1.07 KB

|

None

|

0 👍

/

0 👎

// 0012.go contains some functions to solve ProjectEuler.net's Problem #12
// https://projecteuler.net/problem=12
package main
 
import "fmt"
 
// CountDivisors returns the count of possible divisors of a given number,
// including 1 and itself.
func CountDivisors(number int) int {
    count := 0
    max := number/2
 
    for i := 1; i < max; i++ {
        if number%i == 0 {
            // Increase count by itself + value (number / i)
            count += 2;
 
            // Update max number
            max = number / i
        }
    }
 
    // Return count
    return count
}
 
// FirstTriangularNumber returns the first triangular number with more 
// that the given number of divisors.
func FirstTriangularNumber(numDivisors int) int {
    triangularnumber := 0
    count := 0
 
    for i := 1; count <= numDivisors; i++ {
        // Generate triangular number
        triangularnumber += i
 
        // Update divisor count to divisors in current triangular number
        count = CountDivisors(triangularnumber)
    }
 
    // Return triangular number
    return triangularnumber
}
 
func main(){
    // Display result
    fmt.Println(FirstTriangularNumber(500))
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎