pythagorean

Conversion of Looping to Recursive Solution

时光怂恿深爱的人放手 提交于 2021-02-11 05:12:37
问题 I have written a method pythagoreanTriplets in scala using nested loops. As a newbie in scala, I am struggling with how can we do the same thing using recursion and use Lazy Evaluation for the returning list(List of tuples). Any help will be highly appreciated. P.S: The following method is working perfectly fine. // This method returns the list of all pythagorean triples whose components are // at most a given limit. Formula a^2 + b^2 = c^2 def pythagoreanTriplets(limit: Int): List[(Int, Int,

Conversion of Looping to Recursive Solution

做~自己de王妃 提交于 2021-02-11 05:07:41
问题 I have written a method pythagoreanTriplets in scala using nested loops. As a newbie in scala, I am struggling with how can we do the same thing using recursion and use Lazy Evaluation for the returning list(List of tuples). Any help will be highly appreciated. P.S: The following method is working perfectly fine. // This method returns the list of all pythagorean triples whose components are // at most a given limit. Formula a^2 + b^2 = c^2 def pythagoreanTriplets(limit: Int): List[(Int, Int,

Arrays vs. lists in Lisp: Why are lists so much faster in the code below?

自古美人都是妖i 提交于 2021-01-27 05:50:50
问题 I got an unexpected result while solving Problem 75 in Project Euler. My code does find the correct solution, but it behaves strangely. My solution consists of traversing a Pythagorean tree (Barning's matrices) until the perimeter limit is reached, counting the numbers of times the perimeter assumed each value, and, lastly, counting the perimeter lengths that occurred only once. My admittedly untidy but functioning code is: (defparameter *barning-matrixes* '(#(1 -2 2) #(2 -1 2) #(2 -2 3) #(1

Arrays vs. lists in Lisp: Why are lists so much faster in the code below?

强颜欢笑 提交于 2021-01-27 05:49:46
问题 I got an unexpected result while solving Problem 75 in Project Euler. My code does find the correct solution, but it behaves strangely. My solution consists of traversing a Pythagorean tree (Barning's matrices) until the perimeter limit is reached, counting the numbers of times the perimeter assumed each value, and, lastly, counting the perimeter lengths that occurred only once. My admittedly untidy but functioning code is: (defparameter *barning-matrixes* '(#(1 -2 2) #(2 -1 2) #(2 -2 3) #(1

Working out the distance between two postcodes

拟墨画扇 提交于 2020-01-07 09:07:11
问题 I have looked at other questions that have been answered however, I am still unsure on how to; Get UK postcode data including Longitude, Latitude, Grid-N and Grid-E into my database If I use an API how do I go about it? Where do I start from? Would I need to use Pythagorus Theorem to calculate the distance between the two postcodes? I have got a table in my database for when a user adds a property. Maybe, there is a way when someone adds a property, it can add that postcode along with the

Print a representation of the Pythagorean Triple in C

我怕爱的太早我们不能终老 提交于 2020-01-06 20:17:36
问题 I am trying to create a program that prints the mapping data for found pythagorean triples in C. So far, I have coded the program to be able to find the triples. #include <stdio.h> #include <math.h> int main (int argc, char * argv[]) { int a, b, c; int a2, b2, c2; int limit = 60; for (a = 1; a <= limit; a++) { a2 = a * a; for (b = 1; b <= limit; b++) { b2 = b * b; for (c = 0; c <= ((int)sqrt(a2+b2)); c++) { c2 = c * c; if (a < b && (c2 == (a2 + b2))) { printf("triple: %d %d %d\n", a, b, c); }

Finding the closest points in an array

拈花ヽ惹草 提交于 2020-01-06 06:30:22
问题 I am trying to find not just the closest point, but the 3 closest points in an array of 5 object points. I have tried several experiments using just a distance( d ) variable for each point. But I cannot figure out how to iterate through each point, compare it to the other points using the Pythagorean Theorem/Distance formula, and then find the closest 3. If the array has 5 points, I am guessing I need to store the results of each iteration in an array with a distance ( d ) value, sort by the

Random points inside a circle

寵の児 提交于 2019-12-11 10:36:47
问题 So I got a circle in a Windows Forms Application and have to place 20 random points in this circle. My idea was to split the circle into 4 parts to make it more balanced. My problem is that the points are all generated around the middle and I have no idea how to fix this... Graphics g; Pen p; Random r = new Random(); int[] KegelX = new int[20]; int[] KegelY = new int[20]; private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) {

Having trouble understanding list comprehensions

喜欢而已 提交于 2019-12-08 16:14:39
问题 I've just started learning haskell(literally, tonight!) and I'm having a little trouble understanding the logic of list comprehensions, more specifically the <- operator. A little example on Learn You Some Haskell Finds all tuples that have a length less than 10: ghci> let triangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10] ] my initial understanding was that these would all increment together, but after seeing the output I really dont understanding the incrementing method for

How to calculate Arc length dependently of start angle?

匆匆过客 提交于 2019-12-08 07:02:51
问题 I am building a road editor where one can drag predefined road segments together and combine them to a connected road. One of those segments is a right curve. The curve is drawn as SVG (using D3 arc path function) and has two handles to change the radius and the length directly within the SVG (small black circle changes length and small black square changes the radius). I use a d3 drag handler on the handles. To calculate the new central angle I do as follows: get YOffset from center point of