Solved

Function help needed

Posted on 2013-05-14
6
308 Views
Last Modified: 2013-05-15
If you answering from the MATH forum, you can simply explain your solution rather than using code

I am writing a serial number program.

This will sound like a weird request, but I need a function that will return the following values:

values: 7 , 10, 27, 8, 19, 3 , 21, 30

I don't care what the function body does, so long as it returns those values.

The function parameter is a number from 0 to 9.

I am totally stumped. I just can't figure it out.

Thanks!

function Generate_Code( someInt ){

     // Do something here that will return 7 ( the first number in my list )
    .// based upon the parameter passed.


}

Open in new window


The reason I need those specific values is because they correspond to a keyphrase.


For MATH FORUM EXPERTS, I need ONE SINGLE algorithm that will return the values I listed above. So somehow, if I pass X to your algorithm, it will return 7. Then next time, I pass A DIFFERENT X , it will return 10, etc. I am interested in X, as it will be part of my serial number.
0
Comment
Question by:edvinson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39167149
values: 7 , 10, 27, 8, 19, 3 , 21, 30
The function parameter is a number from 0 to 9.
Do you mean that you want
Generate_Code(0)==7,
Generate_Code(1)==10,
Generate_Code(2)==27,
Generate_Code(3)==8,
Generate_Code(4)==19,
Generate_Code(5)==3,
Generate_Code(6)==21,
Generate_Code(7)==30,
?
But there are two more input parameters from 0 to 9,  and we have run out of specified output values.  What do you want to happen with that?
0
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 39167150
I'm not sure I am understanding this question, but you did state, "I don't care what the function body does, so long as it returns those values" so here goes.  Try this function, and don't forget to include the global variable outside the function:

var $index = 0;


function Generate_Code( $someInt ){

     $array = array(7 , 10, 27, 8, 19, 3 , 21, 30);
     $output = $array[$index];
     $index++;
     if ($index > 7) $index = 0;
     return $output;

}

Open in new window

0
 
LVL 27

Expert Comment

by:aburr
ID: 39167161
The following is a math function which will have your eight numbers as roots.

f(x) = (x-7)(x-10)(x-27)(x-8)(x-19)(x-3)(x-21)(x-30) = 0
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39167704
WTF?  Please do us all a favor and follow the guidance of the SSCCE, so we can see what you really want!  The description omits the information we need about the data structure and desired outputs.  As a result the responses will probably be suboptimal.
http://www.laprbass.com/RAY_temp_edvinson.php
<?php // RAY_temp_edvinson.php
error_reporting(E_ALL);

function EdVinson()
{
    return array(7, 10, 27, 8, 19, 3 , 21, 30);
}

$x = edvinson();
var_dump($x);

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 39168129
If I understand your problem, you are after modular arithmetic:
function Generate_Code( $someInt )
{
	// define desired return values
	$data=array(7 , 10, 27, 8, 19, 3 , 21, 30);
	
	//compute total number of possible values above
	$total=count($data);
	
	//do modular arithemetic on $someInt to determine $index to get the return value
	$index=(intval($someInt,10) % $total);
	
	//return the value in computed $index
return $data[$index];
}

Generate_Code(0);//=>7
Generate_Code(1);//=>10
Generate_Code(2);//=>27
Generate_Code(3);//=>8
Generate_Code(4);//=>19
Generate_Code(5);//=>3
Generate_Code(6);//=>21
Generate_Code(7);//=>30

Generate_Code(8);//=>7
Generate_Code(9);//=>10
Generate_Code(10);//=>27
Generate_Code(11);//=>8
Generate_Code(12);//=>19
Generate_Code(13);//=>3
Generate_Code(14);//=>21
Generate_Code(15);//=>30

Generate_Code(16);//=>7
Generate_Code(17);//=>10
Generate_Code(18);//=>27
Generate_Code(19);//=>8
Generate_Code(20);//=>19
Generate_Code(21);//=>3
Generate_Code(22);//=>21
Generate_Code(23);//=>30
...

Open in new window

0
 
LVL 1

Author Closing Comment

by:edvinson
ID: 39169223
Perfect, this solution is what I described I needed, a function to return the values I listed. The parameters are the serial number.

@RAY I tried to describe my problem as clearly as possible, I apologize if I confused you.

The parameters which return my values, correspond to a PHRASE. Now I can check to see if the serial number is correct by checking the phrase, rather than the actual serial number. This way the number is NOT in my code, which is exactly what I wanted.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article provides a brief introduction to tissue engineering, the process by which organs can be grown artificially. It covers the problems with organ transplants, the tissue engineering process, and the current successes and problems of the tec…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question