-
Notifications
You must be signed in to change notification settings - Fork 1
/
number_decompose.py
94 lines (83 loc) · 1.94 KB
/
number_decompose.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
import rospy
import roslib
import numpy as np
from random import random
from math import sqrt
from std_msgs.msg import String,Int32,Int32MultiArray
final_result = []
raw_result = []
def prime(a):
judge=0
if(a==2):
return True
else:
for j in range(2,round(sqrt(a))+1):
if(a%j==0):
judge=1
break
if(judge==0):
return True
else:
return False
def forprint(N):
for i in range(2,round(sqrt(N))+1,1):
if(N%i==0):
x.append(i)
break
N=N/i
if(prime(N)==False):
decompose(N)
else:
x.append(int(N))
if(len(x)==1):
return N*i
else:
return x
def decompose(N):
for i in range(2,round(sqrt(N))+1,1):
if(N%i==0):
x.append(i)
break
N=N/i
if(prime(N)==False):
decompose(N)
else:
x.append(int(N))
if(len(x)==1):
raw_result = N*i
else:
raw_result = x
t = 0 #Count the position of the number in final_result
for i in range(len(raw_result)):
if raw_result[i]>=10:
temp1 = raw_result[i]%10
temp2 = (raw_result[i]-temp1)/10
final_result(t) = temp1
final_result(t+1) = 10
final_result(t+2) = temp2
t = t+3
if raw_result[i]<10:
final_result[t]
t = t+1
return final_result
def publish():
rospy.init_node('number_decompose')
decompose_publisher = rospy.Publisher('decompose',Int32MultiArray,queue_size=10)
data = Int32MultiArray()
rate = rospy.Rate(10)
while not rospy.is_shutdown():
print "Please enter a number from 1 to 100"
N = raw_input()
data = decompose(N)
rawmsg = forprint(N)
print (N,'=',rawmsg)
msg = Int32MultiArray()
msg = (data)
decompose_publisher.publish(msg)
rate.sleep()
if __name__ == '__main__':
try:
publish()
except rospy.ROSInterruptException:
pass