0% found this document useful (0 votes)
0 views2 pages

Python Code - 5006

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

Python Code - 5006

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 2

def generate_pattern(n):

pattern = []

odd = 3 # Start for odd multiples


even = 2 # Start for even multiples
j=1

while len(pattern) < n:


# Generate multiples of the current odd number
for i in range(1, 4): # Generate 3 multiples of the odd number
pattern.append(odd * i)
if len(pattern) >= n:
return pattern
odd += 2 # Move to the next odd number

# Generate multiples of the current even number


while j <= even: # Generate even number multiples correctly
pattern.append(even * j)
j+=1
if len(pattern) >= n:
return pattern
even += 2 # Move to the next even number, avoiding repetition
j = 1 # reset the counter

return pattern

# Ask the user for input


n = int(input("Enter the number of elements to generate: "))
pattern = generate_pattern(n)
print(pattern)

def generate_pattern(n):
pattern = []

odd = 3
even = 2
i=1
j=1
state = 'odd' # 'odd' for generating odd multiples, 'even' for
generating even multiples
while len(pattern) < n:
if state == 'odd':
# Generate multiples of the current odd number
pattern.append(odd * i)
i += 1
if i > 3:
i=1
odd += 2
state = 'even' # Switch to even multiples

elif state == 'even':


# Generate multiples of the current even number
pattern.append(even * j)
j += 1
if j > even:
j=1
even += 2
state = 'odd' # Switch to odd multiples

# Check if we have reached the desired length


if len(pattern) >= n:
return pattern

# Ask the user for input


n = int(input("Enter the number of elements to generate: "))
pattern = generate_pattern(n)
print(pattern)

You might also like