#204 Code Challenge

#204 Code Challenge

#204 Write some code to count from 1 to 100 incrementing by 1, and identify whether each number is odd, even, or a prime number

Here is some code that counts from 1 to 100, incrementing by 1, and identifies whether each number is odd, even, or a prime number:

def is_prime(n):

    if n < 2:

        return False

    for i in range(2, n):

        if n % i == 0:

            return False

    return True


for i in range(1, 101):

    if i % 2 == 0:

        print(f"{i} is even")

    else:

        print(f"{i} is odd")

    if is_prime(i):

        print(f"{i} is prime")

This code defines a function is_prime that takes an integer n and returns True if n is a prime number, and False if it is not. It then uses a for loop to iterate through the numbers from 1 to 100, and for each number, it prints whether it is odd, even, or prime (or a combination of those).


Write code to produce the fibonacci sequence from 1 to 100, cube them, then take the square root of each

Here is some code that produces the Fibonacci sequence from 1 to 100, cubes each number in the sequence, and then takes the square root of each cube:

import math


def fibonacci(n):

    if n == 1:

        return 1

    elif n == 2:

        return 1

    else:

        return fibonacci(n-1) + fibonacci(n-2)


fib_seq = []

for i in range(1, 101):

    fib_seq.append(fibonacci(i))


cubes = [x**3 for x in fib_seq]

sqrts = [math.sqrt(x) for x in cubes]


print(sqrts)

This code defines a function fibonacci that takes an integer n and returns the nth number in the Fibonacci sequence using recursion. It then uses a for loop to generate the first 100 numbers in the Fibonacci sequence, stores them in a list called fib_seq, and then uses a list comprehension to cube each number in the list and store the result in a new list called cubes. Finally, it uses another list comprehension to take the square root of each number in the cubes list and stores the result in a new list called sqrts. The code then prints the sqrts list.


From <https://chat.openai.com/chat>

Comments

Popular posts from this blog

#1453 Potential Fun Topics to Pursue in Chat

#2024 More: IDEAfier.com

#1512 Table Bots to Play Games