Questions tagged [perfect-square]

Perfect square is an element of algebraic structure which is equal to the square (algebra) of another element.

87 questions
3
votes
0 answers

Rounding error in generating perfect squares python

I am trying to find all the values of n such that (2^n-7) is a perfect square. My code is: import math def isperfect(n): return math.sqrt(n) % 1 == 0 print [i for i in range(3,200) if isperfect(2**(i)-7)] Here is what i am getting: [3, 4,…
3
votes
6 answers

Python - Fastest way to find all perfect squares in a given large number range

I am trying to write a method to get all the perfect squares in a given range in Python. A large range like between 2621163 and 520001400002. Now obviously iterating through the range and checking if a number is perfect like so def is_square(n): …
Vrashabh Irde
  • 14,129
  • 6
  • 51
  • 103
2
votes
1 answer

Restrict search in Prolog - Magic Sqare

I want to solve the Most Perfect Magic Square with a Prolog program. Wiki page: https://en.wikipedia.org/wiki/Most-perfect_magic_square When I input the query "magic_square(4, [[7, 12, 1, 14], [2, 13, 8, 11], [16, 3, 10, 5], [9, 6, 15, 4]])." (which…
2
votes
3 answers

How to print Unique Squares Of Numbers In Java 8?

Here is my code to find the unique number and print the squares of it. How can I convert this code to java8 as it will be better to stream API? List numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5); HashSet uniqueValues = new…
NullPointer
  • 7,094
  • 5
  • 27
  • 41
2
votes
1 answer

Can someone explain to me the runtime of why Perfect Squares is O(sqrt(n))?

Problem Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. Example 1: Input: n = 12 Output: 3 Explanation: 12 = 4 + 4 + 4 Example 2: Input: n = 13 Output: 2 Explanation: 13…
Matt Choi
  • 169
  • 1
  • 13
2
votes
4 answers

Find pairs in two arrays such that when multiplied becomes a perfect square

Given two integer arrays like this:- int[] a = { 2, 6, 10, 13, 17,18 }; int[] b = { 3, 7, 8, 9, 11, 15 }; How can I find pairs from these two arrays such that when multiplied they become perfect square? For eg, in above arrays {2,8} & {18,8}…
Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
2
votes
2 answers

Swift 3 - Find if the Number is a perfect Square

I want to know if there is a way of finding if a number is a perfect square in Swift. I have the user enter a number to check if it is a perfect square. Is there a statement or a function? Thank you in advance!
Epic Gamer_1
  • 104
  • 3
  • 12
2
votes
3 answers

Perfect Square in Leetcode

I am having trouble understanding one of a Leetcode Problem. Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4;…
randy
  • 155
  • 3
  • 9
2
votes
1 answer

perfect square including both end values

I am trying to obtain all the perfect squares between two values(both included). I tried the following code which gives me the count excluding the end values. cin>>a>>b; n=(int)sqrt(b)-sqrt(a); How can i get the count of perfect squares including…
Anantha Raman
  • 197
  • 2
  • 11
2
votes
1 answer

How to check if a number is a perfect square by using mpfr package in R?

I have a determinant which I know is a square of an integer, but because its value is bigger than .Machine$integer.max I used the mpfr package. But I still have problems. Here is the algorithm: > a<- mpfr(sqrt(det(M)), precBits=512);a 1 'mpfr'…
Vassilis Chasiotis
  • 427
  • 1
  • 3
  • 15
2
votes
6 answers

Square of a number in Eclipse not working

This is the code for a calculator app for Android I'm making: package com.example.calculator; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Iterator; import java.util.Stack; import java.lang.Math; import…
2
votes
2 answers

Efficient algorithm to calculate all possible pair whose multiplication is a perfect quare

I have two numbers N and M. I efficiently want to calculate how many pairs of a,b are there such that 1<=a<=N and 1<=b<=M and a*b is a perfect square. I know the obvious N*M algorithm to compute this. But i want something better than that. Thanks…
user1465557
  • 329
  • 2
  • 4
  • 11
2
votes
1 answer

Fast way to test whether n^2 + (n+1)^2 is perfect square

I am trying to program a code to test whether n^2 + (n+1)^2 is a perfect. As i do not have much experience in programming, I only have Matlab at my disposal. So far this is what I have tried function [ Liste ] = testSquare(N) if…
N3buchadnezzar
  • 471
  • 7
  • 12
2
votes
7 answers

Making a list of numbers and their squares in C# using a loop

I want to make a list of numbers and their squares in C# using a for loop. Right now I have: namespace ConsoleApplication { class Program { static void Main(string[] args) { int counter; int square…
Sabotenderizer
  • 187
  • 2
  • 7
  • 13
2
votes
2 answers

Printing square numbers in Visual Basic using loops

I'm trying to print a list of square numbers in Visual Basic using a for loop. I'm a newbie, and find this pretty hard. The program I'm writing is supposed to print a list of the squares of numbers e.g. (1, 4, 9, 16, and so on.)
james.p
  • 21
  • 1
  • 3