• Nov 19, 2022 •CodeCatch
0 likes • 16 views
def sum_of_powers(end, power = 2, start = 1): return sum([(i) ** power for i in range(start, end + 1)]) sum_of_powers(10) # 385 sum_of_powers(10, 3) # 3025 sum_of_powers(10, 3, 5) # 2925
• Sep 9, 2023 •AustinLeath
0 likes • 24 views
print("test")
• May 31, 2023 •CodeCatch
0 likes • 0 views
# Prompt user for a decimal number decimal = int(input("Enter a decimal number: ")) # Convert decimal to binary binary = bin(decimal) # Convert decimal to hexadecimal hexadecimal = hex(decimal) # Display the results print("Binary:", binary) print("Hexadecimal:", hexadecimal)
• Apr 15, 2021 •NoahEaton
import anytree as at import random as rm # Generate a tree with node_count many nodes. Each has a number key that shows when it was made and a randomly selected color, red or white. def random_tree(node_count): # Generates the list of nodes nodes = [] for i in range(node_count): test = rm.randint(1,2) if test == 1: nodes.append(at.Node(str(i),color="white")) else: nodes.append(at.Node(str(i),color="red")) #Creates the various main branches for i in range(node_count): for j in range(i, len(nodes)): test = rm.randint(1,len(nodes)) if test == 1 and nodes[j].parent == None and (not nodes[i] == nodes[j]): nodes[j].parent = nodes[i] #Collects all the main branches into a single tree with the first node being the root for i in range(1, node_count): if nodes[i].parent == None and (not nodes[i] == nodes[0]): nodes[i].parent = nodes[0] return nodes[0]
def generate_floyds_triangle(num_rows): triangle = [] number = 1 for row in range(num_rows): current_row = [] for _ in range(row + 1): current_row.append(number) number += 1 triangle.append(current_row) return triangle def display_floyds_triangle(triangle): for row in triangle: for number in row: print(number, end=" ") print() # Prompt the user for the number of rows num_rows = int(input("Enter the number of rows for Floyd's Triangle: ")) # Generate Floyd's Triangle floyds_triangle = generate_floyds_triangle(num_rows) # Display Floyd's Triangle display_floyds_triangle(floyds_triangle)
# Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number. # For example, if n is 10, the output should be “2, 3, 5, 7”. If n is 20, the output should be “2, 3, 5, 7, 11, 13, 17, 19”. # Python program to print all primes smaller than or equal to # n using Sieve of Eratosthenes def SieveOfEratosthenes(n): # Create a boolean array "prime[0..n]" and initialize # all entries it as true. A value in prime[i] will # finally be false if i is Not a prime, else true. prime = [True for i in range(n + 1)] p = 2 while (p * p <= n): # If prime[p] is not changed, then it is a prime if (prime[p] == True): # Update all multiples of p for i in range(p * 2, n + 1, p): prime[i] = False p += 1 prime[0]= False prime[1]= False # Print all prime numbers for p in range(n + 1): if prime[p]: print (p) # driver program if __name__=='__main__': n = 30 print("Following are the prime numbers smaller") print("than or equal to ", n) print("than or equal to ", n) SieveOfEratosthenes(n)