Amicable | fast algorithm for finding all amicable pairs | Learning library
kandi X-RAY | Amicable Summary
kandi X-RAY | Amicable Summary
This source code implements the leading-edge algorithm for running an exhaustive search for amicable pairs.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Amicable
Amicable Key Features
Amicable Examples and Code Snippets
32 TeRiele 1990
10020507332=2^2*11*31*61*83*1451
10306191676=2^2*11*31*1693*4463
X44 Walker&Einstein 2001
5480828320492525=5^2*7^2*11*13*19*31*17*23*103*1319
5786392931193875=5^3*7*11*13*19*31*37*43*61*809
def solution(n: int = 10000) -> int:
"""Returns the sum of all the amicable numbers under n.
>>> solution(10000)
31626
>>> solution(5000)
8442
>>> solution(1000)
504
>>> solution
public static boolean amicable(int a, int b){
return sum_divisors(a)==b && sum_divisors(b)==a ? true:false;
}
Community Discussions
Trending Discussions on Amicable
QUESTION
I want to have a code that finds amicable numbers in a certain range, but it only outputs one amicable number instead of all amicable numbers in that range.
How can I solve it? I think it may be a scope error.
Amicable numbers are a pair of numbers that the sum of all the divisors of the first number equals the second number, and the sum of the divisors of the second number equals the first number.
Here is my code:
...ANSWER
Answered 2021-May-17 at 11:26I believe you need to move the declarations of sum1
and sum2
inside the second for loop. Check out this fiddle. (Edit: reason is because the sums continue to grow rather than resetting each time.)
Note, I changed a bit of code to ensure that the numbers were distinct (see num1 !== num2
on line 18 of the fiddle) and to avoid repeated pairs (see num2 < num1
rather than num2 < 1300
on line 2 of the fiddle.)
QUESTION
I am trying to write a shell script which will find amicable numbers for given numbers which are entered by user. If found it will print the amicable no else will print "No". I am getting the expected output but it is taking some time. It will be very helpful if code is optimize. I am running the code on git bash.
...ANSWER
Answered 2021-Mar-19 at 21:55Since you're already using awk
in your script, you may as well write the script entirely for awk
. This is a lot faster.
QUESTION
I have a program that computes amicable pairs of numbers up to a certain limit and prints them out to Stdout. FYI - two numbers are amicable if each is equal to the sum of the proper divisors of the other (for example, 220 and 284).
My program works fine for the first 8 or so pairs, but the problem is when the limit gets to a very high number e.g 5 million it becomes very slow. Is there a way to optimize this so that it computes much faster? Here is my code
...ANSWER
Answered 2021-Mar-16 at 04:57You could change the statement of "if(i
QUESTION
I was working on the problem 21 of Project Euler and I have written the following code for the same:
...ANSWER
Answered 2021-Jan-10 at 14:28You can reduce it to run pretty quickly if you run the sumFac
from 2-int(x ** 0.5) + 1
while inserting the divisor
and x/divisor
(doing both work at the same time).
For example, if you run it with x being equal to 16, then we can check whether 2 divides 16 - if it does, we insert both 2 and 16/2=8. etc.. Thus we need to run until the square-root of the number.
Note that if lets say, we have the number x=4
then we do not need to insert 2 twice, so you need to check whether the divisor
and x/divisor
are not equal.
This should be your sumFac
QUESTION
Our task in class today was to find the sum of all amicable numbers from 1-100000.
...ANSWER
Answered 2020-Sep-09 at 22:46You can use a boolean array to keep track of which values you already counted. Here is a working solution:
QUESTION
Sorry for the bad wording, I can't even formulate it in an amicable way, so I'll describe it with examples.
I have a scope of priorities:
...ANSWER
Answered 2020-Aug-06 at 20:07You can
give each item an index, to know their original order. (It might also be possible to rely on sort stability, but it's easier to reason about when being explicit):
QUESTION
I have an unordered list inside another unordered list, surrounded by paragraphs. I have styled my list items to be larger than the paragraphs inside it, but I want the list inside of the list to be the same size as the paragraphs.
Here's an example:
...ANSWER
Answered 2020-Jul-30 at 06:42There are couple of approaches to resolve:
- Use css classes like outerList and innerList. The style will follow and be consistent. Please use your own naming convention. I am not good at them.
- Create a jsfiddle and try different combinations. I guess ul > li ul >li should do. It is not a recommended approach though.
QUESTION
I've written some code to find amicable pairs. Currently, it is very inefficient and I'm trying to fix its problems. As part of this, I have a function areAmicablePairs(a,b)
that does exactly what you'd expect it to do, except that it can't handle cases where a
or b
are 1.
To try and find every amicable pair for a
and b
under some number n
, I've ran which(outer(2:n,2:n,Vectorize(areAmicablePairs)),arr.ind = TRUE)
. It didn't take me long to notice a very obvious inefficiency in how inputs are fed to this function. Specifically, there's repetition. For example, it will check if 100 and 101 are amicable pairs and will later check if 101 and 100 are, an equivalent case.
This gives me my question, without storing a list in memory or making such a list and filtering it down at run time, both of which I assume are very inefficient (I really don't want to store, say, a 20,000 by 20,000 list in memory), how can I feed this vecotrized function the list of unique pairs of numbers from 2 to n? I could make a very carefully-constructed for loop, but the existence of functions like apply
, mapply
, and unique
has me hoping that there's a better way.
Example:
Suppose that I use this as a placeholder for areAmicablePairs
:
ANSWER
Answered 2020-Apr-26 at 16:11You could use combinations()
from gtools
to create such unique pairs (permutations with irrelevance of order). But that will still create a very large matrix of size:
QUESTION
I tried to see what is the pair of the amicable number (under 20000). (amicable number: two different numbers so related that the sum of the proper divisors of each is equal to the other number. (A proper divisor of a number is a positive factor of that number other than the number itself. For example, the proper divisors of 6 are 1, 2, and 3.) So I wrote codes..and debugged.
...ANSWER
Answered 2020-Apr-07 at 09:23I think your code, except amicable
function, shuold be changed logically and also syntactically.
You perform many function calls without a purpose:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Amicable
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page