Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3
- the_number = 1000000000000
- def is_triangular_number_formula(number):
- root_of_square = (number * 8 + 1) ** 0.5
- return root_of_square == round(root_of_square)
- # print(is_triangular_number_formula(the_number))
- # 6 function calls in 0.000 seconds
- def is_triangular_number_algorithm_one(number):
- accumulator = 0
- counter = 0
- while accumulator <= number:
- accumulator += counter
- counter += 1
- if accumulator == number:
- result = True
- break
- else:
- result = False
- return result
- # print(is_triangular_number_algorithm_one(the_number))
- # 5 function calls in 0.474 seconds
- def is_triangular_number_algorithm_two(number):
- counter = 0
- if number == 0:
- return True
- while number > 0:
- counter += 1
- number -= counter
- if number == 0:
- result = True
- break
- else:
- result = False
- return result
- # print(is_triangular_number_algorithm_two(the_number))
- # 5 function calls in 0.419 seconds
- def is_triangular_number_algorithm_three(number):
- for counter in range(number + 1):
- triangular_number = counter * (counter + 1) // 2
- if number == triangular_number:
- return True
- if number < triangular_number:
- return False
- # print(is_triangular_number_algorithm_three(the_number))
- # 5 function calls in 0.679 seconds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement