Is it possible to create a predicate max/2 without an accumulator so that max(List, Max) is true if and only if Max is the maximum value of List (a list of integers)?Yes, you can calculate the maximum after the recursive step. Like: max([M],M). % the

I see that the below code: byte b = 127; b = b + 1; b = b + 1; System.out.println(b); leads to boundary checking during runtime, and the first addition takes b out of range, leading to following error - Main.java:11: error: incompatible types: possib

I wanted to write a program that calculates the sum of all digits of a number. I saw similar posts but none of them were written in JavaScript inside HTML. I am not getting any output, the webpage is entirely blank. <!DOCTYPE html> <html> <

This would be the general problem statement: A prisoner escapes from the jail by jumping over N walls each with height of each wall given in an array. He can jump x meters of height, but after every jump he slips y meters due to some uncontrollable f

There are plenty of addition Vs concatenation questions on internet, but I'm not sure why I'm getting the behavior I am. I'm not trying to "add" obvious strings and I've tried parseInt and parseFloat on all values to no avail. I need to get the

I need to implement an algorithm to drive a Car object. This specific type of car consumes fuel at a higher rate for a while at the beginning of the journey, and at a lower rate from thereon. I'm struggling to implement the algorithm using only integ

I am doing review questions which ask me "What is the output of the following," and I am having some trouble understanding something about this function: int a = 1, b = 1, c = -1; c = --a && b++; printf("%d %d %d", a, b, c); Th

Last week I was in an interview and there was a test like this: Calculate N/9 (given that N is a positive integer), using only SHIFT LEFT, SHIFT RIGHT, ADD, SUBSTRACT instructions.first, find the representation of 1/9 in binary 0,0001110001110001 mea

I'm undertaking an exercise that hinges on being able to do this task. I have to take a multiple digit integer >=0, i.e. 830124, and make an array of the individual digits. My line of thinking is that I can convert it to a string, index the string in

I am a novice in prolog programming, i use swi-prolog. Now I'm stucked by some math problems as we know the predicate :A is 3+3.works well,the answer is A=6. but if I want to find two digits (A and B) from 0~9 that a+b=6 6 is A+B does't work. so I wa

I am debugging a production code written in C and its simplest form can be shown as - void test_fun(int sr) { int hr = 0; #define ME 65535 #define SE 256 sr = sr/SE; <-- This should yield 0 if(sr == 1) hr = ME; else hr = (ME+1)/sr; <-- We should cra

Say I had a really performance-critical loop in my program where I need to check if a point was inside a rectangle, but I know at compile time that the lower bounds are always going to be 0, like the following: (x >= 0 && y >= 0 && x

I want to implement my own (simple) large/arbitrary integer precision arithmetic, first in Java (cause I am more familiar with the syntax), then rewrite it to C. I have addition, subtraction and multiplication for numbers of infinite length and now I

First, this is a homework assignment. I am supposed to create a program using a switch command. It asks the user to input 3 integers, then input an integer of 1-5 for the five cases average, max, min, total, and exit. I have pretty much completed thi

Given a non-negative integer c, I need an efficient algorithm to find the largest integer x such that x*(x-1)/2 <= c Equivalently, I need an efficient and reliably accurate algorithm to compute: x = floor((1 + sqrt(1 + 8*c))/2) (1) For the sake of de

I need to traverse all pairs i,j with 0 <= i < n, 0 <= j < n and i < j for some positive integer n. Problem is that I can only loop through another variable, say k. I can control the bounds of k. So the problem is to determine two arithmeti

OpenBSD's C library has an extension called reallocarray(3) which does realloc(array, size*nmemb) without blowing up if the multiplication overflows. The implementation contains this fragment: /* * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX * if

C language has signed and unsigned types like char and int. I am not sure, how it is implemented on assembly level, for example it seems to me that multiplication of signed and unsigned would bring different results, so do assembly do both unsigned a

I am reading a 24-bit value, presently into a uint32_t variable. After bashing my head against the wall calculating the difference between two such variables, and thinking "it's unsigned! Why is this overflow a problem?!", I realised the values

I need to write program to find the integer square root of a number which is thousands of digits long. I can't use Newton Raphson as I don't have data types to store and divide such large numbers. I am using a long array in C to store the number. Is

In c ~ is 1's complement operator. This is equivalent to: ~a = -b + 1 So, a - ~b -1 = a-(-b + 1) + 1 = a + b – 1 + 1 = a + b Can anyone explains this to me?From elementary school math we know a = -(-a); From twos complement we know that -a = (~a) + 1

I'm trying to multiply two 128-bit integers in C. Here is my algorithm: Split the two 128-bit sequences into S1 and S2. Then split S1 into S11 (front/higher half) and S12 (back/lower half) and split S2 into S21 (front/higher half) and S22 (back/lower

This question already has an answer here: Is floating point math broken? 22 answers I tried this expression in both my FireFox and Chrome console: 17.99 * 100; Expected Result: 1799 Actual Result: 1798.9999999999998 I also tried: parseInt(17.99 * 100

I was a little discouraged such a huge difference in performance when working on 32-bit machines with 32-bit integers in Dart language. Is this means that Dart VM still not optimized for integer arithmetic? Here is my pretty straightforward test. voi

Im having trouble extracting the String data from a stack and converting it to int to perform arithmetic, it keeps telling me that i cannot cast a string to an int and vice versa, however i don't know any other alternative methods. this is the code i

I am reading this in a Lisp textbook: Lisp can perform some amazing feats with numbers, especially when compared with most other languages. For instance, here we're using the function expt to calculate the fifty-third power of 53: CL> (expt 53 53) 24

I want to compute q^k, s.t. q is n bits wide, in the limitations: the final result will be n*k bits wide. for every step of the calculation, the result of multiplying x,y s.t. x is |x| bits wide and y is |y| bits wide is |x|*|y| bits wide. I tried to

Say I have 16 64-bit unsigned integers. I have been careful to feed the carry as appropriate between them when performing operations. Could I feed them into a method to convert all of them into a single string of decimal digits, as though it was one

I have a formula in c that looks like this: X = (a * X) / b; This is used to rescale X with a/b. However X is 16 bit unsigned int and multiplication with a could easily overflow. How could I do this calculation using just integers with an accurate re

Previously today I was trying to add two ushorts and I noticed that I had to cast the result back to ushort. I thought it might've become an uint (to prevent a possible unintended overflow?), but to my surprise it was an int (System.Int32). Is there