When Sandra Lavene began teaching in the Montgomery County public schools 10 years ago, she had visions of unlocking the intricacies of mathematics for bright youngsters in a classroom brimming with enthusiasm.

But not long after she began teaching at Walt Whitman High School in Bethesda, Lavene's vision started to fade. As she expected, the work was hard, the students were sometimes unruly, and the pay was inadequate. What surprised her was the student apathy, the lack of respect for her profession, and the thanklessness.

"I would knock myself out," said Lavene, 34. "I'd stay up long hours marking papers. I worked after school helping the kids. But in nine years, the parents, the principal, the kids -- nobody said 'thanks.'"

She quit her $19,000-a-year teaching job 18 months ago and exchanged the noisy classrooms for quiet computer keyboards, higher pay, merit raises and profit sharing at the Rockville office of Hewlitt-Packard.

Decisions like Lavene's are causing increasing concern among educators and scientists who fear that the difficulty of attracting and keeping math teachers poses a threat not only to schools' ability to offer a full range of math courses but also to the nation's long-term dominance in math-related fields.

The same lures of higher pay and fewer hassles that have prompted an exodus by many math teachers also have reduced sharply the number of students opting for math education degrees in college, with the result that there are serious shortages of math teachers in various areas around the country.

Other areas, including Washington, have managed to maintain an adequate supply of certified math teachers so far. But they are aware of the problem and are scrambling to develop strategies to steer them around possible future shortages.

Many experts, pointing to the growing gap between teachers' salaries and those in private industry, expect the shortages to worsen, primarily in grades 7 through 12.

"A lot of math teachers are going into the computer field simply because it pays more and there are fewer hassles," said Max Sobel, president of the 80,000-member National Council of Teachers of Mathematics. "Math teachers coming out of college already realize that the $12,000 paid to a beginning teacher is a lot less than the $20,000 they could earn in the first year with some computer firms.

"In addition, the public's indifference to the plight of teachers, the low pay, crowded classrooms and poor facilities are a signal to many students to stay from the teaching field," added Sobel, who teaches math at Montclair (N.J.) State College.

Evidence of the problem abounds:

* In North Carolina, 45 percent of all persons teaching math are not certified in the subject. Cleo Meek, assistant director of the mathematics division in the state department of education, said the state's math teachers -- who earn $12,000 to start -- have been hired in large numbers by nearby computer firms that sometimes pay starting salaries of more than $20,000.

* The city of Houston, in a desperate effort to hire more math teachers, has added an $800 bonus to the starting salary of teachers certified to teach math. Still, this year the city issued 44 "emergency permits" to teachers not certified in mathemetics in order to cover its math classes, according to Oscar Sarabia, director for secondary school assignments.

* Philadelphia, which has just come off a crippling 50-day teachers' strike, began the last school year with 90 vacancies for math teachers and ended the year with 24 of those positions unfilled. With a starting salary of only $10,900 for new teachers, school officials said they cannot compete with thriving, talent-hungry computer firms.

* The National Center for Education Statistics last month released its most recent survey on the issue, showing that in 1979 there were 900 math teacher vacancies in elementary and secondary schools nationwide, although there were major surpluses in most other fields.

* A 1979-80 survey in Maryland found that 50,000 secondary school students were being taught mathematics by more than 400 teachers who were not certified to teach math. While that survey has not been updated, state education officials say the situation has not improved since it was taken.

In the Washington area, which has had fewer problems because of generally higher salaries and the lure of the nation's capital, public school systems routinely use persons uncertified in math to teach for a few days or a few weeks while they arrange to fill math vacancies.

Montgomery County, apparently alone among area systems, is allowing half a dozen former elementary school teachers uncertified in math to teach the subject at upper levels -- after they took an aptitude test and promised to take the college courses necessary for certification.

Fairfax County reports no uncertified math teachers, but had been concerned about filling 25 vacancies when the year began. The county is developing a series of college courses that would be taught to teachers in the county schools over two summers and would culminate in a mathematics certification.

D.C. public schools, which have laid off hundreds of teachers in recent years because of declining enrollment and budget cuts, have been able to keep an adequate supply of math teachers by drawing from that pool.

The difficulty of recruiting and retaining math teachers nationwide over the past five years has been paralleled by a similar but somewhat lesser problem with science teachers, but more attention and concern are being focused on math because it is critical to so many disciplines.

"If school systems can't find enough qualified teachers, they will use teachers who do not have a good math background," said Alphonse Buccino, the National Science Foundation's acting deputy assistant director for science and engineering. "Advanced-level mathematics classes which usually have the lowest student enrollment are the first ones to be dropped when there are not enough teachers.

"Then students who might otherwise have studied math in college enter higher education poorly prepared and cannot compete in science and engineering programs. Ultimately, we could find that the pool from which we select our future scientists and engineers is shrinking."

Buccino said that current trends in the United States in which high school students over the last decade have been required to take fewer and fewer math and science courses are directly opposite the approach in Japan, Russia and Germany, where increasingly more math and science studies are required.

At the University of Maryland at College Park, the number of graduating students with math education majors has dropped from 39 eight years ago to only 10 last year, according to Robert Risinger, director of math education at the university. During the same period, the school's business administration, business management and engineering programs became overcrowded, Risinger said.

The same trend has emerged at the University of Virginia in Charlottesville where the number of math education graduates has gone from seven in 1973 to only three this year, according to William Lowery, the university's only remaining math education professor.

"I didn't go into teaching for the money," Lavene said recently. "I wanted to teach kids who had a love for learning math and would be willing to work hard. But after I taught for a few years, I found that the kids wanted good grades, but didn't want to do the work."

Her search for alternative employment led to Hewlett-Packard, where she said she was hired at a salary "significantly higher" than the $19,000 she was earning as a teacher after nine years. Her employment package includes stock purchase options, profit sharing and merit pay increases that brought her pay raises amounting to 25 percent in her first year, she said.

"Mathematics is always a good background for working with computers," said Jerry Hendrick, a spokesman for Hewlett-Packard. "And math teachers make attractive prospective employes because they can usually assimilate computer information rapidly.

"But we don't want to give the impression that we're out to raid the high schools," Hendrick said. "In every instance that we've hired a teacher, they've come to us. If we have an opening and a math teacher applies, we can't say, 'You're needed in the school system, go back there.' "

Lavene's reasons for leaving teaching are echoed by Paris Rasnic. At age 22, he graduated from Virginia Polytechnic Institute with a bachelor's degree in mathematics and immediately took a job teaching math at Marshall High School in Fairfax County.

For the first few years, said Rasnic, he enjoyed teaching. Then the novelty of the classroom wore off and the daily routine of teaching began to wear on his nerves.

"The kids wouldn't cooperate in the classroom," said Rasnic, 32, who is currently employed by a Fairfax County computer firm. "It was a discipline problem that I couldn't control. I frequently came home from school frustrated and mad. That was no way to live."

Rasnic found the contentment he was seeking in a nearby computer firm, which hired him to keep an array of computer programs running smoothly. With few regrets, Rasnic replaced his chalk board, text books and complaining students with a cozy suburban office equipped with computer terminals and silent green screens.

When he went to work for the computer firm in Vienna, Rasnic said, the firm increased the $16,500 a year he was earning after seven years as a teacher by $5,000 in cash and other benefits. He currently earns $27,000 annually, after three years with the company, Rasnic said.

But the money is only one of the benefits of his new job, he said. "I like it here because your supervisors notice what you do and they show appreciation if you've done good work." His job is to keep an array of computer programs running smoothly, and "I can go home relaxed. I don't have to carry my work home with me."

At the end of a typical day, Andre Samson, a math teacher at Woodson High School in Fairfax, leaves work with a briefcase filled with student papers. At home, after dinner, he will typically spend two to three hours marking and evaluating the papers so he can return them to his students the next day.

"When I go home, I don't play with my daughter or talk to my wife or watch television," said Samson, who said he values job satisfaction over wealth. "I grade my papers. I feel I owe it to the kids to get their papers back to them as quick as possible."

As Samson, voted Virginia's "Teacher of the Year" three years ago, has continued his devotion to teaching, he has seen several of his colleagues move into higher paying, less demanding jobs in the computer field.

"I must admit I'm beginning to envy them more and more," he said. "The people who have left teaching come back with a new personality. They say they have fewer pressures and don't have to take their work home."

"Teaching is not a rosy profession," said Samson, 39. "But I'm a professional and the zest for teaching is still in me. But I don't know how long I can continue because the economic pressure has hit me like it has everyone else.

"I work hard and every now and then I get a pat on the back. But a pat on the back won't feed my family."