Skip to content

Commit 7b4183c

Browse files
authored
Add files via upload
1 parent 8f5df4c commit 7b4183c

File tree

5 files changed

+638
-91
lines changed

5 files changed

+638
-91
lines changed

factorization.jl

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
# coding: utf-8
3+
4+
# In[1]:
5+
6+
#global list is the key
7+
#otherwise we will lose the list throughout iterations
8+
global factors=[]
9+
10+
11+
# In[2]:
12+
13+
14+
function factorization(num)
15+
16+
#negative and float should be excluded
17+
if (num<0) || !(isinteger(num))
18+
19+
error("negative or float is not allowed")
20+
21+
end
22+
23+
#if n is smaller than 4
24+
#prime number it is
25+
if num>4
26+
27+
#exclude 1 and itself
28+
#the largest factor of n can not exceed the half of n
29+
#because 2 is the smallest factor
30+
#the range of factors we are gonna try starts from 2 to fld(num,2)+1
31+
#int function to solve the odd number problem
32+
for i in 2:(fld(num,2)+1)
33+
34+
#the factorization process
35+
if num%i==0
36+
37+
push!(factors,i)
38+
39+
#return is crucial
40+
#if the number is not a prime number
41+
#it will stop function from appending non-prime factors
42+
#the next few lines will be ignored
43+
return factorization(Int32(num/i))
44+
45+
end
46+
47+
end
48+
49+
end
50+
51+
#append the last factor
52+
#it could be n itself if n is a prime number
53+
#in that case there is only one element in the list
54+
#or it could be the last indivisible factor of n which is also a prime number
55+
push!(factors,num)
56+
57+
if length(factors)==1
58+
59+
println(num," is a prime number")
60+
empty!(factors)
61+
62+
end
63+
64+
end
65+
66+
67+
# In[3]:
68+
69+
70+
factorization(71392643)
71+
72+
73+
# In[4]:
74+
75+
76+
print(factors)
77+

factorization.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,46 @@
22

33
#global list is the key
44
#otherwise we will lose the list throughout iterations
5-
global list
6-
list=[]
5+
global factors
6+
factors=[]
77

88
def f(n):
9-
#using while function to stop the loop if n is smaller than 4
10-
while int(n/2)>2:
11-
#the factor of n can not exceed the half of n
12-
#assuming 2 is one of the factors
13-
#so the range of factors we are gonna try starts from 2 to int(n/2+1)
9+
10+
global factors
11+
12+
#negative and float should be excluded
13+
assert n>=0 and type(n)!=float,"negative or float is not allowed"
14+
15+
#if n is smaller than 4
16+
#prime number it is
17+
if n>4:
18+
19+
#exclude 1 and itself
20+
#the largest factor of n can not exceed the half of n
21+
#because 2 is the smallest factor
22+
#the range of factors we are gonna try starts from 2 to int(n/2+1)
1423
#int function to solve the odd number problem
1524
for i in range(2,int(n/2)+1):
16-
# the factorization process
25+
26+
#the factorization process
1727
if n%i==0:
18-
list.append(i)
19-
return f(n/i)
20-
#break is to stop infinite loop for prime number
21-
#prime numbers larger than 4 cannot jump outta loop without break
22-
break
28+
factors.append(i)
29+
30+
#return is crucial
31+
#if the number is not a prime number
32+
#it will stop function from appending non-prime factors
33+
#the next few lines will be ignored
34+
return f(int(n/i))
2335

24-
list.append(int(n))
25-
# append the last factor
36+
#append the last factor
2637
#it could be n itself if n is a prime number
2738
#in that case there is only one element in the list
2839
#or it could be the last indivisible factor of n which is also a prime number
29-
if len(list)==1:
40+
factors.append(int(n))
41+
42+
if len(factors)==1:
3043
print('%d is a prime number'%(n))
44+
factors=[]
3145

3246
f(100000097)
33-
print(list)
47+
print(factors)

palindrome checker 4 methods.jl

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
2+
# coding: utf-8
3+
4+
# In[1]:
5+
6+
#check if a string is a palindrome
7+
#reversing a string is extremely easy
8+
#at first i was thinking about using pop function
9+
#however, for an empty string, pop function is still functional
10+
#it would not stop itself
11+
#it would show errors of popping from empty list
12+
#so i have to use the classic way, slicing
13+
#each time we slice the final letter
14+
#to remove the unnecessary marks, we have to use regular expression
15+
function palindrome_slicing(rawtext)
16+
17+
#regex is very different in julia
18+
#we need to use match attribute to get string
19+
text=[i.match for i in eachmatch(r"[a-zA-Z0-9]",lowercase(rawtext))]
20+
21+
#use non recursive slicing
22+
if text[length(text):-1:1]==text
23+
24+
return true
25+
26+
else
27+
28+
return false
29+
30+
end
31+
32+
end
33+
34+
35+
# In[2]:
36+
37+
38+
function palindrome_recursion(text)
39+
40+
#this is the base case, when string is empty
41+
#we just return empty
42+
if isempty(text)
43+
44+
return text
45+
46+
else
47+
48+
#such a pain without a python style list+list
49+
return cat([text[end]],
50+
palindrome_recursion(text[1:end-1]),
51+
dims=1)
52+
53+
end
54+
55+
end
56+
57+
58+
# In[3]:
59+
60+
61+
function recursion_check(rawtext)
62+
63+
#this part is the regular expression to get only characters
64+
#and format all of em into lower cases
65+
text=[i.match for i in eachmatch(r"[a-zA-Z0-9]",lowercase(rawtext))]
66+
67+
if palindrome_recursion(text)==text
68+
69+
return true
70+
71+
else
72+
73+
return false
74+
75+
end
76+
77+
end
78+
79+
80+
# In[4]:
81+
82+
#or creating a new list
83+
#using loop to append popped items from the old list
84+
function palindrome_list(rawtext)
85+
86+
text=[i.match for i in eachmatch(r"[a-zA-Z0-9]",lowercase(rawtext))]
87+
88+
new=String[]
89+
90+
for i in 1:length(text)
91+
92+
#be careful with append
93+
#append will create char type instead of string type
94+
push!(new,text[end-i+1])
95+
96+
end
97+
98+
if new==text
99+
100+
return true
101+
102+
else
103+
104+
return false
105+
106+
end
107+
108+
end
109+
110+
111+
# In[5]:
112+
113+
#alternatively, we can use deque
114+
#we pop from both sides to see if they are equal
115+
#if not return false
116+
#note that the length of string could be an odd number
117+
#we wanna make sure the pop should take action while length of deque is larger than 1
118+
function palindrome_deque(rawtext)
119+
120+
text=[i.match for i in eachmatch(r"[a-zA-Z0-9]",lowercase(rawtext))]
121+
122+
while length(text)>=1
123+
124+
if pop!(text)!=popfirst!(text)
125+
126+
return false
127+
128+
end
129+
130+
end
131+
132+
return true
133+
134+
end
135+
136+
137+
# In[6]:
138+
139+
140+
#0.257236 seconds (636.24 k allocations: 31.108 MiB, 2.37% gc time)
141+
@time begin
142+
143+
recursion_check("Evil is a deed as I live!")
144+
145+
end
146+
147+
148+
# In[7]:
149+
150+
151+
#0.058568 seconds (87.67 k allocations: 4.231 MiB)
152+
@time begin
153+
154+
palindrome_slicing("Evil is a deed as I live!")
155+
156+
end
157+
158+
159+
# In[8]:
160+
161+
162+
#0.084145 seconds (140.27 k allocations: 6.717 MiB, 8.51% gc time)
163+
@time begin
164+
165+
palindrome_list("Evil is a deed as I live!")
166+
167+
end
168+
169+
170+
# In[9]:
171+
172+
173+
#0.062403 seconds (74.95 k allocations: 3.500 MiB)
174+
@time begin
175+
176+
palindrome_deque("Evil is a deed as I live!")
177+
178+
end
179+
180+
181+
# In[10]:
182+
183+
184+
#in julia, the fastest is still the slicing approach
185+

0 commit comments

Comments
 (0)