Computation of factorial in SciLab - factorial

This Scilab function is supposed to compute the factorial of n. But it gives an error stated below:
function fac(n)
if (n<=0) then n = 1
else
n = n* fac(n-1)
end
endfunction
The errors are stated here :
-->fac(23)
!--error 44
Wrong argument #2.
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
at line 5 of function fac called by :
fac(23)

You forgot the return value n = in the very first line just after function, it will never return anything otherwise
function n = fac(n)
if (n<=0) then n = 1
else
n = n* fac(n-1)
end
endfunction

Related

F# I cannot make a countdown from 10 to 0

I'm quite new to F# and wanted to attempt to make a simple countdown; however, in the code below, it tells me there is something wrong with the "t..0".
let countdown x =
let mutable t = 10
for t..0 do
t=t-1
I want it to countdown in the terminal from 10 to 0.
There are three issues with your snippet. First, you do not need to decrement the t in a for loop - this happens automatically. Second, if you want a range like t .. 0 to go down, you need to specify -1 as the step. Also, your syntax for a for loop needs to define a variable (and you do not need to do this outside of the loop):
let countdown x =
for t in 10 .. -1 .. 0 do
printfn "Counting: %d" t
Tomas' answer shows the range syntax, but there's also the imperative syntax, analogous to for loops.
for i = 10 downto 1 do
printfn "Counting: %d" i
For counting up, use for i = 0 to 10.
Try this:
var i = 10;
var interval = setInterval( increment, 1000);
function increment(){
i = i - 1;
if(i <= -1) {
return
}
console.log(i);
}
Breakdown:
Where the countdown should start:
var i = 10;
The increment interval: (usually 1000ms)
var interval = setInterval( increment, 1000);
The actual countdown:
function increment(){
i = i - 1;
If counter goes into negative numbers, stop counting:
if(i <= -1) {
return
}
Write result in the console log:
}
console.log(i);
}

How can I fix this Array?

#!/bin/python3
b = []
def rotate(d,a):
if d == 0:
return a
b = []
for i in range(len(a)):
b.insert(i-1,a[i])
a = b
rotate(d-1,b)
if __name__ == '__main__':
d = 4
a = ['1','2','3','4','5']
print(rotate(d,a))
I don't know why but when i return 'a' it it prints none. Can anyone help me with this
your issue is the missing return statement when rotate is called recursively:
def rotate(d,a):
if d == 0:
return a
#....
rotate(d-1,b) # <---- here
if you call rotate(d, a) with d > 0, you enter the recursion rotate(d-1,b)...but you throw away the result and python instead returns None. this is because in python any method call that doesn't explicitly return a value, always returns None...and your initial call to rotate(d, a) with d > 0 never passed by a return statement (only the recursive call with d = 0 does, but you throw away that result)...so it returns None.
this should fix your problem:
def rotate(d,a):
if d == 0:
return a
#....
return rotate(d-1,b) # propagate the calculated value back up
see also: Why does my recursive function return None?

Trouble with CTypes - calling C dll function

I have the following C function exported from a dll
typedef struct _TStubMethod
{
TBoolean stubAtEnd;
TBoolean longStub;
} TStubMethod;
int JpmcdsStringToStubMethod
(char *name,
TStubMethod *stubMethod
);
In Python, I have defined the type & function and called the function as follows:
class TStubMethod(Structure):
_fields_ = [
('stubAtEnd', c_int),
('longStub', c_int)
]
def JpmcdsStringToStubMethod(dll, name, stubmethod):
func = dll.JpmcdsStringToStubMethod
func.argtypes = [POINTER(c_char), POINTER(TStubMethod)]
func.restype = c_int
return func(name, stubmethod)
stubFS = TStubMethod(False, False)
ret = JpmcdsStringToStubMethod(dll, 'F/S', byref(stubFS))
I am getting an error as follows. What am I doing wrong?
ArgumentError Traceback (most recent call last)
<ipython-input-61-ec7303283f89> in <module>
25
26 stubFS = TStubMethod(False, False)
---> 27 ret = JpmcdsStringToStubMethod(dll, 'F/S', byref(stubFS))
<ipython-input-55-c8ab0da16b04> in JpmcdsStringToStubMethod(dll, name, stubmethod)
9 func.argtypes = [POINTER(c_char), POINTER(TStubMethod)]
10 func.restype = c_int
---> 11 return func(name, stubmethod)
ArgumentError: argument 1: <class 'TypeError'>: wrong type
Thanks for your help.
When passing zero-terminated strings, use c_char_p. According to [Python 3]: class ctypes.c_char_p:
Represents the C char * datatype when it points to a zero-terminated string.
Translated to your code:
Change the function argtypes (1st one):
func.argtypes = [c_char_p, POINTER(TStubMethod)]
Use bytes (which is Python's equivalent for char*). You can either (not both):
Converting string to bytes (to keep changes in JpmcdsStringToStubMethod):
return func(name.encode(), stubmethod)
Pass a bytes instance to JpmcdsStringToStubMethod:
ret = JpmcdsStringToStubMethod(dll, b"F/S", byref(stubFS))

Understanding loess delta1 and delta2 combination

I am trying to track down how to get to delta1 and delta2 in the R simpleLoess function that is called by the loess function.
In the simpleLoess function there is a call
z <- .C(C_loess_raw, y, x, if (no.st) 1 else weights,
if (no.st) weights * robust else 1, D, N, as.double(span),
as.integer(degree), as.integer(nonparametric), as.integer(order.drop.sqr),
as.integer(sum.drop.sqr), as.double(span * cell),
as.character(surf.stat), fitted.values = double(N),
parameter = integer(7L), a = integer(max.kd), xi = double(max.kd),
vert = double(2L * D), vval = double((D + 1L) * max.kd),
diagonal = double(N), trL = double(1L), delta1 = double(1L),
delta2 = double(1L), as.integer(surf.stat == "interpolate/exact"))
Which calls some C code which itself calls some fortran code. Then the delta1 and delta2 are just extracted as:
one.delta <- z$delta1
two.delta <- z$delta2
That is called in the c function here. That itself calls some fortran here. At this point I'm at a loss -- any suggestions on how the actual delta1 and delta2 are calculated so that I can recreate it would be very helpful...

Create a Genie Array

Hi I have a Problem with Arrays.
I have class Arr the var a = new array of int[100] is the problem.
The example works but wenn I put the var a ... behind class Arr (return) (Tab) i have this error message :
arr.gs:3.2-3.4: error: syntax error, expected declaration but got `var' with previous `tab indent'
What's the problem. Sorry for my english , sorry I don't understand the editor
thanks
class Arr
def arr_test()
var a = new array of int [100]
i : int = 0
for i = 0 to 99
a[i] = i
for i = 0 to 99
print "%4d",a[i]
init
Intl.setlocale()
var v = new Arr()
v.arr_test()
I have tried your code in my computer and no problem, but the format of the indent was wrong.
Genie code indentation can be writed with tabs or spaces;
If you uses spaces you must to explicit how many... like this
[indent=4] at the start of the code. Like here http://manualgenie.blogspot.com.es/
But if you want to use tabs instead spaces (is more confortable) you must to ensure there is not spaces before any code line. Like here: http://genie.webierta.skn1.com/wiki/colecciones
For Vala/genie programming I use Geany editor and it has an option for replace all spaces in tabs, or all tabs in spaces in "Document" option of the task bar.
When the problem is how to use o where use "var" I will explain here:
Var is used for declare and define one identifier(variable) in only one code line and for be used temporaly. But if you want to have global scope into the class, getting it useful for all the "def" procedures of the class, you must declare at the start of the class. Like the example above. Also, if we use "init" for declare it the class must be defined as "GLib.Object"
class Arr:GLib.Object
a : array of int [] //declare
init
a = new array of int [100] //define
def arr_test()
i : int = 0
for i = 0 to 99
a[i] = i
for i = 0 to 99
print "%4d",a[i]
init
var v = new Arr()
v.arr_test()
Aslo, you can declare it but define after in your "def" procedures. Like in this example:
class Arr
a : array of int []
def arr_test()
a = new array of int [100]
i : int = 0
for i = 0 to 99
a[i] = i
for i = 0 to 99
print "%4d",a[i]
def arr_test2()
a = new array of int [120]
i : int = 0
for i = 0 to 119
a[i] = i
for i = 0 to 119
print "%4d",a[i]
init
var v = new Arr()
v.arr_test()
v.arr_test2()
Note: In this case we don't use "init", therefore is not needed the declaration :GLib.Object.
Thanks,
my Problem is not the tab or space. I have checked this.
This is the Problem :
class Arr
var a = new array of int [100]
def ...
init
...
... and so on.
It is one Tab no space.
Thanks

Resources