Integer modulus division and integer division remainder with Ballerina - wso2

How to do,
integer modulus division and
integer division remainder
operations with Ballerina?

You can get the modulus by using / operator and remainder using % operator.
Check the following code:
import ballerina/io;
public function main() {
int i = 10;
int j = 3;
int x = i / j;
int y = i % j;
io:println("i / j = " + x.toString());
io:println("i % j = " + y.toString());
}
This will result the following:
i / j = 3
i % j = 1

Related

Integer Division?

Regarding the code shown below:
#include <cmath>
int main()
{
const int n = 10000;
const int K = 10;
double* matrix = new double[n * n];
for(int k = 0; k < K; ++k) {
for(int j = 0; j < n; ++j) {
for(int i = 0; i < n; ++i) {
double ai = (double)i/double(n);
double aj = (double)i/double(n);
matrix[i * n + j] += pow(n, (double)k / K) / exp((double)k / K) * pow(sin(ai),2) * pow(sin(aj),2);
}
}
}
}
Is the way that lines
double ai = (double)i/double(n);
double aj = (double)i/double(n);
are defined is because we want floating-point division as opposed to integer division?
In addition, why are the operands cast in the way they are, i.e (double)i/double(n) instead of double(i)/double(n)?
Yes because if i and n are two integers as follow:
int i = ...;
int n = ...;
double ai = i/n
This will be an integer division. Say i=5 and n=9, even ai being a double it will end up with 0 as result.
You can either cast i or n to tell the compiler that you want a float division.
Yes. Actually, it would be enough to cast just one of the operands, but some programmers prefer to cast all the operands for consistency and more clarity.

I don't understand the how prime numbers are calculated and modPow function is working in the following code

A Magic Fraction for N is one that has the following properties:
It is a proper fraction (The value is < 1)
It cannot be reduced further (The GCD of the numerator and the denominator is 1)
The product of the numerator and the denominator is factorial of N. i.e. if a/b is the fraction, then a*b = N!
Examples of Magic Fractions are:
1/2 [ gcd(1,2) = 1 and 1*2=2! ]
2/3 [ gcd(2,3) = 1 and 2*3=3! ]
3/8 [ gcd(3,8) = 1 and 3*8=4! ]
2/12 for example, is not a magic fraction, as even though 2*12=4!, gcd(2,12) != 1
And Magic fractions for number 3 are: 2/3 and 1/6 (since both of them satisfy the above criteria, are of the form a/b where a*b = 3!)
Now given a number N, you need to print the total number of magic fractions that exist, for all numbers between 1 and N (include magic fractions for N, too).
Can anybody tell me what is modPow function doing?
Refer the link to see the question, that will give an idea why this code.
using namespace std;
#define ll long long int
#define S(n) scanf("%lld", &n)
ll MOD = 1e18 + 7;
ll modPow(ll a, ll b)
{
ll res = 1;
a %= MOD;
for (; b; b >>= 1) {
if (b & 1)
res = res * a % MOD;
a = a * a % MOD;
}
return res;
}
int main()
{
ll i, j;
ll va = 1;
ll sum = 0;
ll prime[1000] = { 0 };
for (i = 2; i <= 500; i++) {
if (prime[i] == 0)
for (j = 2 * i; j <= 500; j += i)
prime[j] = 1;
}
ll val[600] = { 0 };
val[1] = 0;
val[2] = 1;
ll co = 0;
for (i = 3; i <= 500; i++) {
if (prime[i] == 0) {
co++;
}
ll t1 = modPow(2, co);
val[i] = t1 + val[i - 1];
val[i] %= MOD;
// cout << i << " " << val[i] << "\n";
}
ll n;
S(n);
cout << val[n] << "\n";
}

optimization for nested loop c++

Can someone skilled in loops say how this task can be optimized - at least a bit? Maybe some cache? or loop un switching?
memory limit 512 m
#include <iostream>
using namespace std;
const int MOD = 1000000007;
int main() {
int n;
cin >> n;
int x = 0;
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
for (int k = i; k <= j; ++k) {
x = (x + 1) % MOD;
}
}
}
cout << x << endl;
}
Any help is appreciated
A simple WolframAlpha query gives you a closed-form expression for the loop that you are calculating:
x = (1/6 * n * (n + 1) * (n + 2)) % MOD
Please give us MathJax support on StackOverflow!
Then, it's just a matter of turning that closed-form expression into code. Because we do not want x to overflow, we must occasionally perform modulo before the end of the operation:
long long x = n % MOD;
x = (x * (n + 1)) % MOD;
x = (x * (n + 2) / 6) % MOD;
With this, the number of mathematical operations is independent of the size of n, and certainly much faster than your loop.

How to implement division by addition?

An interview question.
How to implement division by addition? suppose they are all int.
My idea
Add divisor to itself until it is larger than dividend.
Each iteration, keep the sum result before addition.
The quotient is the sum result before the last addition. the remainder can be counted by adding 1 until the quotient * divisor + reminder == dividend.
It is O(e^n), any better ideas? bit operation?
dividing m by n:
int r = m;
int q = 0;
while( r >= n )
{
int k = 1;
int x = n;
int t;
while( ( t = x+x ) < r )
{
x = t;
k += k;
}
q += k;
r -= x;
}
The result is q - quotient, r - remainder.
The idea is that x+x is the same as x*2.
UPD:
Some may complain that r -= x is not addition.
Well we may update the algorithm to not use subtraction:
int p = 0;
int q = 0;
while( p+n <= m )
{
int k = 1;
int x = n;
int t;
while( p + ( t = x+x ) < m )
{
x = t;
k += k;
}
q += k;
p += x;
}
The result is q - quotient.
If we need the remainder then we proceed as follows (p - output from the above):
int r = 0;
while( p < m )
{
int x = 1;
int t;
while( p + ( t = x+x ) < m )
{
x = t;
}
r += x;
p += x;
}
The result is r - remainder.
The algorithm has obviously polynomial (not exponential) running-time.
In digital arithmetic we can name restoring and non-restoring methods as simple division algorithms which are based on addition/subtraction. Number of iterations in these methods are of O(n) (where n is the number of bits). There are methods like Newton-Raphson or reciprocal calculation which are based on multiplication and number of iterations in them are of O(log n). Take a look at http://en.wikipedia.org/wiki/Division_%28digital%29
You would break the division into its logarithmic components and then compute those.
For int numbers you could use this logic:
16 divided by 5 = 3 (i)
i=1 -> 5+0 = 5 < 16
i=2 -> 5+5 = 10 < 16
i=3 -> 5+5+5 = 15 < 16
i=4 -> 5+5+5+5 = 20 > 16
So 3 will be your answer.

exp function using c++

I can't figure out why I keep getting the result 1.#INF from my_exp() when I give it 1 as input. Here is the code:
double factorial(const int k)
{
int prod = 1;
for(int i=1; i<=k; i++)
prod = i * prod;
return prod;
}
double power(const double base, const int exponent)
{
double result = 1;
for(int i=1; i<=exponent; i++)
result = result * base;
return result;
}
double my_exp(double x)
{
double sum = 1 + x;
for(int k=2; k<50; k++)
sum = sum + power(x,k) / factorial(k);
return sum;
}
You have an integer overflow in your factorial function. This causes it to output zero. 49! is divisible by 2^32, so your factorial function will return zero.
Then you divide by it causing it to go infinity. So the solution is to change prod to double:
double prod = 1;
Instead of completely evaluating the power and the factorial terms for each term in your expansion, you should consider how the k'th term is related to the k-1'th term and just update each term based on this relationship. That will avoid the nasty overflows in your power and factorial functions (which you will no longer need). E.g.
double my_exp(double x)
{
double sum = 1.0 + x;
double term = x; // term for k = 1 is just x
for (int k = 2; k < 50; k++)
{
term = term * x / (double)k; // term[k] = term[k-1] * x / k
sum = sum + term;
}
return sum;
}
you should just reduce max of k form 50 to like 30 it will work;
and one question your code work just near 0 ?