Trouble with OCaml nested if - if-statement

With my original code I kept getting Error: Parse error: [expr level ;] expected after "in" (in [expr]) on the line let numDigits = numDigits - 1 in
Original:
let rec rev_int num =
if num / 10 == 0 then
num
else
let temp = num mod 10 in
let numDigits = String.length(string_of_int num) - 1 in
if num < 0 then
let numDigits = numDigits - 1 in
else
let numDigits = numDigits + 0 in
let num = (num - temp) / 10 in
temp * int_of_float(10.0 ** float_of_int numDigits) + rev_int num
With variations of:
if num < 0 then
let numDigits = numDigits - 1 in;
else
let numDigits = numDigits + 0 in;
if num < 0 then
let numDigits = numDigits - 1 in
else begin
let numDigits = numDigits + 0 in end
I revised the code and now it works, but I was wondering if there was a way to do it with nested if and less redundancy.
Revised:
let rec rev_int num =
if num / 10 == 0 then
num
else
let temp = num mod 10 in
let numDigits = String.length(string_of_int num) - 1 in
if num < 0 then
let numDigits = numDigits - 1 in
let num = (num - temp) / 10 in
temp * int_of_float(10.0 ** float_of_int numDigits) + rev_int num
else
let numDigits = numDigits + 0 in
let num = (num - temp) / 10 in
temp * int_of_float(10.0 ** float_of_int numDigits) + rev_int num

From Hunan Rostomyan
let rec rev_int num =
if num / 10 == 0 then
num
else
let temp = num mod 10 in
let numDigits = String.length(string_of_int num) - 1 in
let numDigits = numDigits - (if num < 0 then 1 else 0) in
let num = (num - temp) / 10 in
temp * int_of_float(10.0 ** float_of_int numDigits) + rev_int num

Related

If m % n == k or m % n == l then print 1, else print any other number

4 integers are given (all no more than 10^6): m, n, k, l. If m % n == k or m % n == l, then print 1, else any other number. Conditional operators cannot be used!
Examples:
12 8 3 4 // input
1 // output
0 5 1 2 // input
0 // output
I wrote this code:
#include <iostream>
using namespace std;
int main()
{
int m, n, k, l;
cin >> m >> n >> k >> l;
cout << ((1 / (((m % (n + 1 / (n + 1))) - k) * ((m % (n + 1 / (n + 1))) - k) + 1)) - 1) * ((1 / (((m % (n + 1 / (n + 1))) - l) * ((m % (n + 1 / (n + 1))) - l) + 1)) - 1) + 1;
return 0;
}
But it does not work for all cases. For example, 0, 0, 0, 0 gives 1, but should give any other number.
Please help.
Note that there is no answer for n == 0 because division by zero is undefined.
In the other cases, since true prints as "1" and false as "0" by default,
cout << (m % n == l || m % n == k);
should do it.
what i understand from your question you are checking for 2 conditions, so after taking the input from the user we can check those conditions and assign value of val integer to 1 if that condition is true, else it will be 0 as initialized.
int m, n, k, l;
int val =0;
cin >> m >> n >> k >> l;
if(m % n == k || m % n == l){
val = 1;
}
cout << val ;
From what I understand I think you are trying to compare the modulus of two numbers with the other two inputs and if the result matches you want 1 as output otherwise 0
this will help you achieve it
#include <iostream>
using namespace std;
int main()
{
int m, n, k, l;
cin >> m >> n >> k >> l;
if(m%n == k || m%n == l)
{
count << 1
}
else
{
count << 0
}
return 0;
}

nested for loop optimisation

I have this expression
for (size_t i = 0; i < expression.size(); i++){
for (size_t j = i + 1; j < expression.size(); j++){
result += (expression.at(j) - expression.at(i));
}
result += (g - expression.at(i));
}
return result;
in the vector expression we have for example [1,2,3]. I am trying to get something like:
f1=[(2-1)+(3-1)]
r1 = g-1
h1 = r1+f1
f2=[3-2]
r2 = g-2
h2 = r2+f2
f3 = 0
r3 = g-3
h3 = r3+f3
then h1+h2+h3
What i am doing right now is in Θ(n^2). Is there a way to make it faster even without for loops?
Addition is commutative and associative so the operations can be reordered and grouped without changing the final result. (Note: not taking into account possible overflows in the intermediate calculations, which may be affected by the order and grouping of operations.)
In pseudo-code with n = expression.size() and x[k] = expression.at(k) the original code can be broken down as follows, with the intermediate results indicated in comments.
a = b = c = d = 0
for i = 0 to (n-1)
for j = (i+1) to (n-1)
a += x[j]
// == x[i+1] + x[i+2] + ... x[n-1]
// a == 0 * x[0] + 1 * x[1] + 2 * x[2] + 3 * x[3] + ... + (n-1) * x[n-1]
for i = 0 to (n-1)
for j = (i+1) to (n-1)
b += x[i];
// == (n-i-1) * x[i]
// b == (n-1) * x[0] + (n-2) * x[1] + ... + 2 * x[n-3] + 1 * x[n-2]
for i = 0 to (n-1)
c += g
// c == n * g
for i = 0 to (n-1)
d += expression.at(i))
// d == x[0] + x[1] + ... + x[n-1]
result = c + a - b - d
= n * g
+ (0 - (n-1) - 1) * x[0]
+ (1 - (n-2) - 1) * x[1]
+ ...
+ ((n-2) - 1 - 1) * x[n-2]
+ ((n-1) - 0 - 1) * x[n-1]
The latter result can be calculated directly from that formula, with one single O(n) loop.

How to write this recursion with loops

I saw the following as an exercise in a website. It basically says write the following function without using recursion and without using structures like vector, stack, etc:
void rec(int n) {
if (n != 0) {
cout << n << " ";
rec(n-1);
rec(n-1);
}
}
At first I thought it was going to be easy, but I'm suprisingly struggling to accomplish it.
To understand it better, I defined it like a math function as the following:
f(x) = {1 if x = 0, f(x-1) + f(x-1) otherwise} (where + operator means concatenation and - is the normal minus)
However, Unrolling this made it harder, and I'm stuck. Is there any direct way to write it as a loop? And also, more generally, is there an algorithm to solve this type of problems?
If you fiddle with it enough, you can get at least one way that will output the ordered sequence without revisiting it :)
let n = 5
// Recursive
let rec_str = ''
function rec(n) {
if (n != 0) {
rec_str += n
rec(n-1);
rec(n-1);
}
}
rec(n)
console.log(rec_str)
// Iterative
function f(n){
let str = ''
for (let i=1; i<1<<n; i++){
let t = i
let p = n
let k = (1 << n) - 1
while (k > 2){
if (t < 2){
break
} else if (t <= k){
t = t - 1
p = p - 1
k = k >> 1
} else {
t = t - k
}
}
str += p
}
console.log(str)
}
f(n)
(The code is building a string, which I think should be disallowed according to the rules, but only for demonstration; we could just output the number instead.)
void loop(int n)
{
int j = 0;
int m = n - 1;
for (int i = 0; i < int(pow(2, n)) - 1; i++)
{
j = i;
if (j == 0)
{
std::cout << n << " ";
continue;
}
m = n - 1;
while (true)
{
if (m == 1)
{
std::cout << m << " ";
m = n - 1;
break;
}
if (j >= int(pow(2, m)))
{
j = j - int(pow(2, m)) + 1;
}
if (j == 1)
{
std::cout << m << " ";
m = n - 1;
break;
}
else
{
j--;
}
m--;
}
}
std::cout << std::endl;
}
For n = 3 for instance
out = [3 2 1 1 2 1 1]
indexes = [0 1 2 3 4 5 6]
Consider the list of indexes; for i > 0 and i <= 2^(m) the index i has the same value as the index i + 2^(m)-1 where m = n - 1. This is true for every n. If you are in the second half of the list, find its correspondent index in the first half by this formula. If the resulting number is 1, the value is m. If not, you are in a lower level of the tree. m = m - 1 and repeat until the index is 1 or m =1, in which case you've reached the end of the tree, print 1.
For instance, with n = 4, this is what happens with all the indexes, at every while step. p(x) means the value x gets printed at that index. A / means that index has already been printed.:
n = 4,m = 3
[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
m = 3
[p(n=4) 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
if(i >=2^3) -> i = i -2^3 + 1)
[/ 1 2 3 4 5 6 7 1 2 3 4 5 6 7]
if(i == 1) -> print m, else i = i -1
[/ p(3) 1 2 3 4 5 6 p(3)1 2 3 4 5 6]
m = 2
if (i >=2^2) -> i = i - 2^2 +1
[/ / 1 2 3 1 2 3 / 1 2 3 1 2 3]
if(i == 1) -> print m, else i = i -1
[ / / p(2) 1 2 p(2) 1 2 / p(2) 1 2 p(2) 1 2]
m = 1
if (m == 1) -> print(m)
[ / / / p(1) p(1) / p(1) p(1) / / p(1) p(1) / p(1) p(1)]
Therefore the result is:
[4 3 2 1 1 2 1 1 3 2 1 1 2 1 1]
void via_loop(int n) {
string prev = "1 ", ans = "1 ";
for (int i = 2; i <= n; i++) {
ans = to_string(i) + " " + prev + prev;
prev = ans;
}
cout << ans;
}
Idea is to save result from the previous computation of each number. Full code:
void rec(int n) {
if (n != 0) {
cout << n << " ";
rec(n-1);
rec(n-1);
}
}
void via_loop(int n) {
string prev = "1 ", ans = "1 ";
for (int i = 2; i <= n; i++) {
ans = to_string(i) + " " + prev + prev;
prev = ans;
}
cout << ans;
}
int main() {
int n = 5;
cout << "Rec : ";
rec(n);
cout << endl;
cout << "Loop: ";
via_loop(n);
cout << endl;
}
Output:
Rec : 5 4 3 2 1 1 2 1 1 3 2 1 1 2 1 1 4 3 2 1 1 2 1 1 3 2 1 1 2 1 1
Loop: 5 4 3 2 1 1 2 1 1 3 2 1 1 2 1 1 4 3 2 1 1 2 1 1 3 2 1 1 2 1 1

Partition function based on Euler's formula

I am trying to find the partitions of a number using the Euler's formula for that:
It produces results like:
P(3) = P(2) + P(1) = 3
P(4) = P(3) + P(2) = 3+ 2 = 5
P(5) = P(4) + P(3) - P(0) = 5 + 3 - 1 = 7
P(6) = P(5) + P(4) - P(1) = 7 + 5 - 1 = 11 and so on..
* P(0) = 1
It produces two positive and then two negative values and so on.
I am using recursion for that but the code goes into an infinite loop without producing any result.
long result = 0;
long counter = 0;
class Euler
{
public:
long Partition(long n)
{
int exponent = 0;
if (n < 0)
{
return 0;
}
else
{
counter = counter + 1;
exponent = pow(-1, counter - 1) ;
if (n == 0)
{
n = 1;
}
return Partition((exponent * (n - ( (counter * ( (3 * counter) - 1)) / 2)))) +
Partition(((exponent * (n - ( (counter * ( (3 * counter) + 1)) / 2)) )));
}
}
};
int main(int argc, char** argv)
{
long result= 0;
long a = 3;
Euler * obj = new Euler();
long s = obj->Partition(a);
std::cout << s;
return 0;
}
Your global counter is modified by the first call to Partition, so the second one operates on a different one; in fact, the counter changes more or less unpredictably.
Do not use globals.

converting a string of numbers to binary

If the string is "8123", I first converted the string number into its integer form 8123 and then sent this number to a function that converts it to binary. I got numbers as big as unsigned long long to work but once its passed that, the outputs are wrong. Is there a way to convert to binary by looking at each digit.
i.e looking a the 3, 2, 1, and 8 to convert to binary.
So rather than taking the string "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" and turning it into a number, is there a way to look at each character in this string and turn it into binary?
Any suggestions is much appreciated
Pseudocode:
string binary_string = ""
#an example
number = 81
while (number != 0)
#append the string casted value of the remainder of (number / 2)
#to the front of binary_string
binary_string = str(number % 2) + binary_string
number = number / 2
e.g. 81:
binary_string = str(81 % 2) + binary_string = str(1) + "" = "1"
number = 81 / 2 = 40
binary_string = str(40 % 2) + binary_string = str(0) + "1" = "01"
number = 40 / 2 = 20
binary_string = str(20 % 2) + binary_string = str(0) + "01" = "001"
number = 20 / 2 = 10
binary_string = str(10 % 2) + binary_string = str(0) + "001" = "0001"
number = 10 / 2 = 5
binary_string = str(5 % 2) + binary_string = str(1) + "0001" = "10001"
number = 5 / 2 = 2
binary_string = str(2 % 2) + binary_string = str(0) + "10001" = "010001"
number = 2 / 2 = 1
binary_string = str(1 % 2) + binary_string = str(1) + "010001" = "1010001"
81 -> "1010001"
string dec2bin(string in) {
for(size_t i = 0; i < in.length(); i++)
in[i] -= '0';
string out;
while(in.length()) {
out.insert(0, 1, '0' + (in[in.length()-1]&1));
char overflow = 0;
if(in[0]<=1) {
overflow = 10;
in.erase(0);
}
for(size_t i = 0; i<in.length(); i++) {
in[i] += overflow;
overflow = 10 * (in[i]&1);
in[i] /= 2;
}
}
return out;
}