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

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

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

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

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

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

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

Add two numbers without using the addition operator

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

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

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