Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 4 of 4

04102014, 03:34 PM #1
 Join Date
 Mar 2014
 Posts
 15
 Thanks
 0
 Thanked 0 Times in 0 Posts
Gender based on factors for my math story
I am writing a Math World story where the first numbers are 0100 and the rest are given birth to. I am trying to assign them genders and this has worked so far:
0 is male
1 is female
2 is male
3 is female
4 is hermaphroditic(male and female at the same time) because it has 1 and 2 as well as itself as factors and there is a 50/50 ratio between male factors and female factors in this case
5 is male
I was just assigning them without much of a function but as you can see I am alternating genders with the primes.
I want to make 2 functions that do 2 things.
1) I want to make a function that alternates gender with each prime
2) I want to make a function that does all these comparisons:
male factors > female factors = male
female factors > male factors = female
female factors = male factors = hermaphroditic
for composite numbers including 1 but not itself as a factor.
I realize I might have to nest these in a trial division primality test and I might need to do several iterations so that I get factors of factors of factors... but still if I could make a program that finds all the factors including primes of a number and than do either the alternating if it is prime or the comparison if composite to assign genders of numbers between 0 and 100 that would be nice.
I originally posted something about this on a forum about excel but realized that I could really only give a really good guess if I didn't have a good program that could do iterations of the factorization process. 1 of the questions that I have is how can I make the factorization only happen when some variable x is composite and if I need to do another iteration take those factors I got and run them through the primality test again to see if any are composite and get the factors of any composite factors.
Also how can I make a function that alternates gender with each prime number in sequence like 2 is male, 3 is female, 5 is male, 7 is female etc. when x in the primality test is prime?
04102014, 05:53 PM
#2
 Join Date
 Jun 2007
 Location
 Urbana
 Posts
 4,600
 Thanks
 11
 Thanked 616 Times in 596 Posts
i don't quite get your criteria for what makes a number hermaphroditic, but i'll try to help anyway.
you need a function that finds factors of numbers. these are often found in/around isPrime() code examples.
you can loop that isPrime 0100 and save the output to build your initial list of gender flip points, or whatever logic you're using (i don't understand that part).
however you do it, make a list of the numbers and their genders.
now for the calculator function:
i would go with a cached data approach for speed and simplicity.
you can actually use this calculator function itself to build it's cache of numbers.
the key here is that we avoid a lot of repetitive calculations and function calls by not using a recursive solution.
put all your male numbers into a pseudosparse array so that you have lots of zeros and the relevant numbers are at their natural index.
for example, using the primes: [0,0,2,3,0,5,0,7,0,9,0,11,0,13,0,0,0,17 ... ].
do the same for female and hermaphroditic numbers, building up to the square root of the highest unknown number you want to verify.
now, for a given new number X:
make a vote counter tally for male (for the final number, not the factors)
find the factors of X, put them in an array
forEach each element in that new factor array, maleCount= males.slice(X).filter(Number).length, femaleCount=femails.slice(X).filter(Number).length
if maleCount > femailCount then add one vote for male
after all factors have been iterated, compare the male tally to the length of the factor array divided by two.
if they are equal, the new number is hermaphroditic, else male>half=male, vice versa...
the speedy part is using [].slice and [].filter(Number), both of which are very fast native functions that avoid recursion and big execution stacks.
Last edited by rnd me; 04102014 at 06:04 PM.
Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com
04112014, 12:49 AM
#3
 Join Date
 Mar 2014
 Posts
 15
 Thanks
 0
 Thanked 0 Times in 0 Posts
for male numbers the first one is 0 and the next one is the first prime or 2 and every other prime after 2 is male. Thus 5, 11, 17... are male. Any composite male number has more of these male primes in its factors including factors of composite factors than female primes.
For female numbers the first one is 1 and the next one is 3 and every other prime after 3 is female. Thus 7,13,19... are female. Any female composite number has more of these female primes in its factors including factors of composite factors than male primes.
Hermaproditic numbers have an equal amount of male and female factors.
Why only up to the square root of the number I want to verify when numbers can easily have factors above that square root for the pseudo sparse arrays? I mean I can understand it in a primality test because to prove that a number is prime you only need to go up to sqrt n but why in the first 3 arrays? Is that because any other factors are either composites of at least 2 of the other factors or primes larger than sqrt n?
04112014, 06:00 PM
#4
 Join Date
 Jun 2007
 Location
 Urbana
 Posts
 4,600
 Thanks
 11
 Thanked 616 Times in 596 Posts
you will find half the factor combinations by going up the square root. the other half is the compliment to those factors. so if we were doing 10, we only need to count up to 4 to find all the factors, because 5 will be found via 2, and since it's communitive, [5,2] is the same as [2,5]...
Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com