## Prolog: predicate for the maximum without accumulator

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

## Binary addition checks type limits while shorthand and increment do not

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

## sum of all digits of a number in JavaScript and HTML

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> <

## Explanation of the test case in the prisoner's wall jump program

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

## Unusual concatenation behavior of adding JavaScript

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

## Implement the algorithm with only integer arithmetic, several fuel consumption rates for a car

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

## Explanation of the output of this function

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

## How to divide by 9 using just changes / add / sub?

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

## How: integer to multiple digits, extract each integer in an array of integers

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

## prolog how to use mathematical operation

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

## The entire 32-bit signed division gives 0x7fffffff as quotient on PowerPC

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

## Does the punting type signed with unsigned integers allow you to check limits faster by eliminating the need for & gt; = comparison?

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

## Large Integer Arithmetic - How to implement the module?

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

## Notepad can not find the symbol for average, max, min, etc.

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

## Fast integer solution of x (x-1) / 2 = c

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

## From a loop index k, get the pairs i, j with i & lt; j?

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

## Calculate sqrt (SIZE_MAX + 1) using only integer constant expressions, restoring strange ABI

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

## signed and unsigned arithmetic implementation on x86

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

## C unsigned int comparison - wrap around custom bit number

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

## What is the efficient algorithm for finding the entire square root of a very large number, digit by digit?

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

## Multiply two 128-bit entries

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

## JavaScript multiplication arithmetic

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

## Is it possible to write in Dart the high-performance code that works on 32-bit machines with 32-bit integers?

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

## How to display string data in an entire container to perform arithmetic operations

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

## What gives Lisp its excellent performance in mathematics?

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

## Exponentiation in an uncomplicated way

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

## Printing multiple integers as an arbitrarily long decimal string

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

## How to resize an int using two possibly larger numbers in the formula (a * X) / b

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

## Why is ushort + ushort equal to int?

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