I am very difficult to display all the output results.
this code.
DEF VAR INPUTAN AS INTEGER.
DEF VAR i AS INTEGER.
DEF VAR j AS INTEGER.
DEF VAR a AS INTEGER.
DEF VAR rows AS INT.
DEF VAR pascal AS CHAR FORMAT "x(25)".
SET INPUTAN.
a = 1.
REPEAT i = 0 TO INPUTAN:
rows = i.
DISPLAY rows.
REPEAT j = 0 TO i :
IF j = 0 OR j = i THEN DO:
a = 1.
END.
ELSE
a = a * (i + 1 - j) / j.
pascal = STRING(a).
display a.
END.
END.
DEF VAR INPUTAN AS INTEGER.
DEF VAR i AS INTEGER.
DEF VAR j AS INTEGER.
DEF VAR a AS INTEGER.
DEF VAR rows AS INT.
DEF VAR pascal AS CHAR.
SET INPUTAN.
a = 1.
REPEAT i = 0 TO INPUTAN:
rows = i.
/*DISPLAY rows. */
REPEAT j = 0 TO i :
IF j = 0 OR j = i THEN DO:
a = 1.
END.
ELSE
a = a * (i + 1 - j) / j.
IF j = 0 THEN
pascal = pascal + FILL(" ", INPUTAN - i).
pascal = pascal + STRING(a) + " ".
IF j = i THEN
pascal = pascal + CHR(13).
/* display a.*/
END.
END.
MESSAGE pascal
VIEW-AS ALERT-BOX INFO BUTTONS OK.
Related
How would I create a module that will accept a 2d array of integers and return only the items above a certain count?
interface TestIfc;
method Action putInput(??? _input);
method ActionValue#(???) getOutput;
endinterface
module mkTest (TestIfc);
Vector#(PE_N, FIFO#(Vector#(Max_itemN, Item))) inputQ <- replicateM(mkFIFO);
Vector#(PE_N, FIFO#(Vector#(Max_itemN, Item))) accQ <- replicateM(mkFIFO);
FIFO#(Vector#(Max_itemN, Item)) mergeQ <- mkFIFO;
FIFO#(Tuple2#(Vector#(Max_itemN, Item), Bit#(32))) outputQ <- mkFIFO;
rule count;
let row1 <- input???.first; input???.deq;
Vector#(???, item) sum = replicate(0);
for(Bit#(8) j = 0; j < ???; j = j + 1)
begin
let n <- row1[j];
sum[n] <= sum[n] + 1;
end
accumulator.enq(sum);
endrule
rule filter;
accumulator.deq;
let acc_t = accumulator.first;
Int cnt = 0;
Vector#(???, Item) result = replicate(0);
for(Bit#(8) i = 0; i < ???; i = i + 1)
begin
if (acc_t[i] > SOMEBUMBER)
begin
result[cnt] = i;
cnt = cnt + 1;
end
end
endrule
...
I'm thinking of maybe splitting my input data vertically or horizontally to do more work efficiently, but right now I'm not confident in creating even a simple version and I'd like some help.
I've been trying to create a function in GDScript to process and calculate a string using PEMDAS rules. Below is my try on the subject. It can so far only use the MDAS rules:
Is there a better way to achieve such a function?
func _ready() -> void:
### USE CASES ###
print(Compute_String("1+2*3+3=")) # Output = 10
print(Compute_String("1+2*3*3=")) # Output = 19
print(Compute_String("1*2*3+3=")) # Output = 9
print(Compute_String("1+2+3*3=")) # Output = 12
print(Compute_String("5*2+7-3/2=")) # Output = 15.5
print(Compute_String("9+5.5*2.25=")) # Output = 21.375
print(Compute_String("5*2+7-3/2")) # Output = 1.#QNAN (Missing equals)
print(Compute_String("5*2+7-/2=")) # Output = 1.#QNAN (Adjacent operators)
print(Compute_String("*2+7-3/2=")) # Output = 1.#QNAN (Begins with operator)
print(Compute_String("")) # Output = 1.#QNAN (Empty)
print(Compute_String("=")) # Output = 1.#QNAN (Considered as empty)
print(Compute_String("1 +2=")) # Output = 1.#QNAN (Contains space)
print(Compute_String("(1+2)*3=")) # Output = 1.#QNAN (Parentheses not supported)
func Compute_String(_string: String) -> float:
var _result: float = NAN
var _elements: Array = []
if not _string.empty() and _string[_string.length() - 1] == "=":
var _current_element: String = ""
for _count in _string.length():
if _string[_count].is_valid_float() or _string[_count] == ".": _current_element += _string[_count]
else:
if _string[_count - 1].is_valid_float() and (_string[_count + 1].is_valid_float() if _string[_count] != "=" else true):
_elements.append_array([_current_element,_string[_count]]) ; _current_element = ""
else: return NAN
if not _elements.empty():
_elements.resize(_elements.size() - 1)
while _get_operators_count(_elements) != 0:
var _id: Array = [0, 0.0, 0.0]
if "*" in _elements:
_id = _add_adjacent(_elements, "*") ; _remove_adjacent(_elements, _id[0]) ; _elements.insert(_id[0] - 1, _id[1] * _id[2])
elif "/" in _elements:
_id = _add_adjacent(_elements, "/") ; _remove_adjacent(_elements, _id[0]) ; _elements.insert(_id[0] - 1, _id[1] / _id[2])
elif "+" in _elements:
_id = _add_adjacent(_elements, "+") ; _remove_adjacent(_elements, _id[0]) ; _elements.insert(_id[0] - 1, _id[1] + _id[2])
elif "-" in _elements:
_id = _add_adjacent(_elements, "-") ; _remove_adjacent(_elements, _id[0]) ; _elements.insert(_id[0] - 1, _id[1] - _id[2])
else: return NAN
if _elements.size() == 1: _result = _elements[0]
return _result
func _get_operators_count(_elements: Array) -> int:
var _result: int = 0 ; for _element in _elements: if not str(_element).is_valid_float(): _result += 1 ; return _result
func _add_adjacent(_elements: Array, _operator) -> Array:
return [_elements.find(_operator), float(_elements[_elements.find(_operator) - 1]), float(_elements[_elements.find(_operator) + 1])]
func _remove_adjacent(_elements: Array, _operator_idx: int) -> void:
_elements.remove(_operator_idx + 1) ; _elements.remove(_operator_idx) ; _elements.remove(_operator_idx - 1)
Im currently working on a project about the multiple encryption method! I am having a lot of trouble with RSA. I have a code that encrypt, give the public and the private key. Now I need to let someone write the private key and the encrypted text, and make the program decrypt it. I tried many times, ando got so many different erros that I deleted the decrypt function to do it over from start. Could anyone shine some ligth upon me? How to do, what should I do... Any help, really.
This is the code:
import random
def totient(number):
if(prime(number)):
return number-1
else:
return False
def prime(n):
if (n <= 1):
return False
if (n <= 3):
return True
if (n%2 == 0 or n%3 == 0):
return False
i = 5
while(i * i <= n):
if (n%i == 0 or n%(i+2) == 0):
return False
i+=6
return True
def generate_E(num):
def mdc(n1,n2):
rest = 1
while(n2 != 0):
rest = n1%n2
n1 = n2
n2 = rest
return n1
while True:
e = random.randrange(2,num)
if(mdc(num,e) == 1):
return e
def generate_prime():
while True:
x=random.randrange(1,100)
if(prime(x)==True):
return x
def mod(a,b):
if(a<b):
return a
else:
c=a%b
return c
def cipher(words,e,n):
tam = len(words)
i = 0
lista = []
while(i < tam):
letter = words[i]
k = ord(letter)
k = k**e
d = mod(k,n)
lista.append(d)
i += 1
return lista
def calculate_private_key(toti,e):
d = 0
while(mod(d*e,toti)!=1):
d += 1
return d
## MAIN
if __name__=='__main__':
text = input("Insert message: ")
p = generate_prime() # generates random P
q = generate_prime() # generates random Q
n = p*q # compute N
y = totient(p) # compute the totient of P
x = totient(q) # compute the totient of Q
totient_de_N = x*y # compute the totient of N
e = generate_E(totient_de_N) # generate E
public_key = (n, e)
print('Your public key:', public_key)
text_cipher = cipher(text,e,n)
print('Your encrypted message:', text_cipher)
d = calculate_private_key(totient_de_N,e)
print('Your private key is:', d)
How can I write this algorithm using iteration?
function generate(num1:byval)
if num1 > 10 then
return 10
else
return num1 + (generate(num1 + 1) DIV 2)
endif
endfunction
So it's not straight forward so I start by doing some grunt work:
n result
11.. 10
10 10
9 9 + 10/2
8 8 + (9 + 10/2)/2
7 7 + (8 + (9 + 10/2)/2)/2
This looks like a pattern.. While the recursive version started on the input and went upwards it's easy to see that by starting at 10 and going downwards one can simply update the accumulator by halving the value and adding the current value.
This can easily be implemented using a helper:
procedure generate(num : integer) : integer
begin
generate := generateHelper(10, num, 0)
end
procedure generateHelper(cur : integer, num: integer, acc : integer) : integer
begin
if cur = num then
generateHelper := cur + acc/2;
else
generateHelper := generateHelper(cur - 1, num, acc/2 + cur);
end
Or by using a loop:
procedure generate(num : integer) : integer
var cur, acc : integer;
begin
for cur := 10 downto cur do
acc := acc / 2 + cur;
generate := acc;
end
If you work out some values for the function…
f(10) = 10
f(9) = 9+f(10)/2 = 9+10/2 = 14
f(8) = 8+f(9)/2 = 8+14/2 = 15
…
You will get a sense that you could repeatedly apply the same formula to a value in a loop. You see if you start from 10, you divide by 2 and add 9, then divide by 2 and add 8, and keep going until you reach the number given to the function. That would look something like this, e.g. in JavaScript:
function generate(n) {
let x = 10;
for(let i = 10; i > n; i--) {
x = i - 1 + x / 2;
}
return x;
}
I have a recursive function in SML that does a certain computation that doesn't really matter for my question. What I want to do is I want to track the number of times the recursion has taken place, as in I want to count the iterations of my algorithm. I know if for example I declared:
val counter = 0;
val counter = counter + 1;
The other counter is a different variable. It is not the same one incremented by one. So this type of incrementing will lose its scope in one recursive call.
Is there any way I can keep track?
You can use an int ref as a mutable cell:
val counter : int ref = ref 0
-- Some dummy recursive function for testing
fun factorial n =
if n < 1
then 1
else (counter := !counter + 1; n * factorial (n - 1))
You can use it like this:
- !counter;
val it = 0 : int
- factorial 10;
val it = 3628800 : int
- !counter;
val it = 10 : int
- factorial 5;
val it = 120 : int
- !counter;
val it = 15 : int