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

Wirth

This Python code file contains multiple functions for generating and iterating through the Wirth sequence of numbers, including functions that use recursion, iteration, and searching to determine if a number is in the Wirth sequence. Additional functions generate the first n numbers of the Wirth sequence by alternating between the numbers generated by dividing by 2 and 3.

Uploaded by

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

Wirth

This Python code file contains multiple functions for generating and iterating through the Wirth sequence of numbers, including functions that use recursion, iteration, and searching to determine if a number is in the Wirth sequence. Additional functions generate the first n numbers of the Wirth sequence by alternating between the numbers generated by dividing by 2 and 3.

Uploaded by

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

/home/pablo/Dropbox/02 - Docencia/Paradigmas de Programación/code/python/wirth.

py

1 def es_wirth(n):
2 if n == 1:
3 return True
4 elif (n-1) % 2 == 0 and es_wirth((n-1)/2):
5 return True
6 elif (n-1) % 3 == 0 and es_wirth((n-1)/3):
7 return True
8 else:
9 return False
10
11 def iter_wirth(n):
12 i, k = 1, 0
13 while k < n:
14 if es_wirth(i):
15 yield i
16 k = k + 1
17 i = i + 1
18
19 def iter_wirth2(n):
20 wirth = []
21 i = 1
22 while len(wirth) < n:
23 if i == 1 or (i%2==1 and (i-1)/2 in wirth) or (i%3==1 and (i-1)/3 in wirth):
24 wirth.append(i)
25 yield i
26 i = i + 1
27
28 def iter_wirth3(n):
29 wirth = []
30 i = 1
31 while len(wirth) < n:
32 if i == 1 or (i%2==1 and binary_search(wirth,
(i-1)/2)) or (i%3==1 and binary_search(wirth,(i-1)/3)):
33 wirth.append(i)
34 yield i
35 i = i + 1
36
37 def iter_primeros_wirth2(n):
38 L2 = [1]
39 L3 = [1]
40 for k in range(n):
41 if L2[0] == L3[0]:
42 x = L2[0]
43 L2.pop(0)
44 L3.pop(0)
45 elif L2[0] < L3[0]:
46 x = L2[0]
47 L2.pop(0)
48 else:
49 x = L3[0]
50 L3.pop(0)
51 yield x
52 L2.append(2*x + 1)
53 L3.append(3*x + 1)
54
55 def binary_search(A,x):
56 i = 0
57 j = len(A) - 1
58 while i < j:
59 m = (i+j)/2

1 of 2 02/21/2018 09:53 PM
/home/pablo/Dropbox/02 - Docencia/Paradigmas de Programación/code/python/wirth.py

60 if x > A[m]:
61 i = m + 1
62 else:
63 j = m
64 return x == A[i]
65

2 of 2 02/21/2018 09:53 PM

You might also like